{"id":19720883,"url":"https://github.com/piasy/safelyandroid","last_synced_at":"2025-09-01T14:42:42.841Z","repository":{"id":75583073,"uuid":"55334732","full_name":"Piasy/SafelyAndroid","owner":"Piasy","description":"Build safely Android app, no more Activity not found, Activity state loss and NPE during fragment transaction!","archived":false,"fork":false,"pushed_at":"2016-10-27T03:57:13.000Z","size":2903,"stargazers_count":399,"open_issues_count":0,"forks_count":42,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-02T12:07:53.814Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://blog.piasy.com/2016/04/03/Safely-Android/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Piasy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-04-03T07:21:44.000Z","updated_at":"2025-02-23T05:42:47.000Z","dependencies_parsed_at":"2023-06-06T23:45:25.235Z","dependency_job_id":null,"html_url":"https://github.com/Piasy/SafelyAndroid","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Piasy%2FSafelyAndroid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Piasy%2FSafelyAndroid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Piasy%2FSafelyAndroid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Piasy%2FSafelyAndroid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Piasy","download_url":"https://codeload.github.com/Piasy/SafelyAndroid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054211,"owners_count":21039952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T23:12:49.560Z","updated_at":"2025-04-09T14:07:27.503Z","avatar_url":"https://github.com/Piasy.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SafelyAndroid\n[ ![Download](https://api.bintray.com/packages/piasy/maven/SafelyAndroid/images/download.svg) ](https://bintray.com/piasy/maven/SafelyAndroid/_latestVersion)\n\nBuild safely Android app, no more Activity not found, Activity state loss and NPE during fragment transaction!\n\n## Pause-safe fragment transaction\n![safely-android-demo.gif](art/safely-android-demo.gif)\n\n## Dependency\nAdd to gradle dependency of your module build.gradle:\n\n```gradle\nrepositories {\n    jcenter()\n}\n\ndependencies {\n    compile 'com.github.piasy:safelyandroid:1.2.0'\n}\n```\n\n## Usage\n\n### Start activity safely\nStart activity from context, fragment and support v4 fragment, and start activity for result from\nactivity, fragment and support v4 fragment, with or without the optional bundle options.\n`StartActivityDelegate` will launch the intent only when its component exist, otherwise, it will\nreturn `false`. Never get activity not found error again!\n\n```java\nStartActivityDelegate.startActivitySafely(fragment, intent)\n\nStartActivityDelegate.startActivitySafely(fragment, intent, options)\n\nStartActivityDelegate.startActivityForResultSafely(fragment, intent, requestCode)\n\nStartActivityDelegate.startActivityForResultSafely(fragment, intent, requestCode, options)\n\nStartActivityDelegate.startActivitySafely(context, intent)\n\nStartActivityDelegate.startActivitySafely(context, intent, options)\n\nStartActivityDelegate.startActivityForResultSafely(activity, intent, requestCode)\n\nStartActivityDelegate.startActivityForResultSafely(activity, intent, requestCode, options)\n```\n\n### Dismiss dialog fragment safely\nDismiss a dialog fragment or support v4 dialog fragment safely. `dialogFragmentDismissDelegate`\nwill dismiss this dialog fragment only when it's resumed, otherwise, it will return `true` and\ndismiss it when `dialogFragmentDismissDelegate.onResumed` is called. Never get activity state\nloss error again!\n\n```java\ndialogFragmentDismissDelegate.safeDismiss(dialogFragment)\n\ndialogFragmentDismissDelegate.onResumed(dialogFragment)\n\nsupportDialogFragmentDismissDelegate.safeDismiss(dialogFragment)\n\nsupportDialogFragmentDismissDelegate.onResumed(dialogFragment)\n```\n\n### Do fragment transaction safely\nDo fragment or support v4 fragment transaction safely. Components (activity, fragment or support\nv4 fragment) should implement the `TransactionCommitter` interface, and the fragment transaction\nwill only be committed when the components is resumed, otherwise it will return true and the\ntransaction will be committed when `onResumed` is called. Never get activity state loss error again!\n\n```java\nfragmentTransactionDelegate.safeCommit(transactionCommitter, transaction)\n\nfragmentTransactionDelegate.onResumed()\n\nsupportFragmentTransactionDelegate.safeCommit(transactionCommitter, transaction)\n\nsupportFragmentTransactionDelegate.onResumed()\n```\n\n### Build fragment transaction safely\n\nSee `FragmentTransactionBuilder` and `SupportFragmentTransactionBuilder`.\n\n### Just extends the safely base components\nWe provide the default safely components, including `SafelyActivity`, `SafelyDialogFragment`,\n`SafelyFragment`, `SafelyAppCompatActivity`, `SafelySupportDialogFragment`, and\n`SafelySupportFragment`, which encapsulate safely behaviours above, you can just extends them, and\ndismiss dialog fragment with `safeDismiss()`, do fragment transaction with\n`safeCommit(transaction)`, then you can build safely Android app now!\n\n### Create your own base components\nIf your base components must extends other base components, you can make your base component\nimplement the `TransactionCommitter` interface, and mimic the behaviours of\n[safely base components](https://github.com/Piasy/SafelyAndroid/tree/master/safelyandroid/src/main/java/com/github/piasy/safelyandroid/component)\nprovided by us, then you can also build safely Android app now!\n\n## Lint check\nWhen using `safeCommit`, lint will warn \"Missing `commit()` calls\", you could disable it.\n\nWe provide a custom lint rule, to check the \"unsafe `dismiss()` call\" of dialog fragment.\n\n![safely-android-demo.gif](art/safely-android-lint.png)\n\nJust adding this dependency in your module's `build.gradle`:\n\n```gradle\nrepositories {\n    jcenter()\n}\n\ndependencies {\n    compile 'com.github.piasy:safelyandroid-lint:1.2.0'\n}\n```\n\nThe suppress of \"Missing `commit()` calls\" and \"unsafe `commit()` call\" lint rules may not be added in the long future. :(\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiasy%2Fsafelyandroid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiasy%2Fsafelyandroid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiasy%2Fsafelyandroid/lists"}