{"id":20543854,"url":"https://github.com/chinalike/activityresultapi","last_synced_at":"2025-04-14T09:40:40.582Z","repository":{"id":55057095,"uuid":"470078400","full_name":"ChinaLike/ActivityResultApi","owner":"ChinaLike","description":"Activity Result API 扩展，解决before they are STARTED，支持ARouter","archived":false,"fork":false,"pushed_at":"2022-12-23T07:36:24.000Z","size":171,"stargazers_count":36,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-27T22:51:15.494Z","etag":null,"topics":["activity-result-api","arouter","before-they-are-started"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ChinaLike.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-15T08:59:43.000Z","updated_at":"2024-10-17T07:29:10.000Z","dependencies_parsed_at":"2023-01-30T18:01:24.521Z","dependency_job_id":null,"html_url":"https://github.com/ChinaLike/ActivityResultApi","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChinaLike%2FActivityResultApi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChinaLike%2FActivityResultApi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChinaLike%2FActivityResultApi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChinaLike%2FActivityResultApi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ChinaLike","download_url":"https://codeload.github.com/ChinaLike/ActivityResultApi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248855578,"owners_count":21172600,"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":["activity-result-api","arouter","before-they-are-started"],"created_at":"2024-11-16T01:41:02.249Z","updated_at":"2025-04-14T09:40:40.558Z","avatar_url":"https://github.com/ChinaLike.png","language":"Kotlin","readme":"# ActivityResultApiKx\n[![](https://img.shields.io/badge/platform-android-brightgreen.svg)](https://developer.android.com/index.html)  [![API](https://img.shields.io/badge/API-17%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=17)  [![](https://jitpack.io/v/ChinaLike/ActivityResultApi.svg)](https://jitpack.io/#ChinaLike/ActivityResultApi)  [![Gradle-4.1.2](https://img.shields.io/badge/Gradle-4.1.2-brightgreen.svg)](https://img.shields.io/badge/Gradle-4.1.2-brightgreen.svg)  [![](https://img.shields.io/badge/language-kotlin-brightgreen.svg)](https://kotlinlang.org/)\n\n官方已明确标记`startActivityForResult()`为`@deprecated`，并给出`@link`指定使用`registerForActivityResult`，即Activity Result API，下面我们来看一张图：\n\n![QQ20220318-100841@2x.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e279b6f56059485e85ee9963897f0279~tplv-k3u1fbpfcp-watermark.image?)\n在这个大环境下，想必很多人也会把新项目或者升级老项目使用Activity Result API，如果还未使用或者想使用的人可以先了解一下Activity Result API的基础使用，但是使用的人知道存在以下几个问题：\n\n+ java.lang.IllegalStateException: LifecycleOwner XXXX is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.\n\n+ ARouter不支持Activity Result API（目前不支持，不代表以后不支持）\n\n基于以上问题，本文给出了解决方案\n\n# API特色\n\n+ 优雅的适配Activity Result API，真正的支持在非Activity和Fragment中调用\n+ 支持不用在onResume()之前初始化ActivityResultLauncher\n+ 支持[ARouter](https://github.com/alibaba/ARouter)\n+ 支持Kotlin和Java用法\n+ Kotlin扩展，使用简单\n\n# 如何使用\n\n\u003e step 1.在根目录的build.gradle添加：\n\n```\nallprojects {\n   repositories {\n      ...\n      maven { url \"https://jitpack.io\" }\n   }\n}\n```\n\n\u003e step 2.然后在 build.gradle(Module:XXX) 的 dependencies 添加：\n\n当前最新版本：[![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2df70399060a48678061b6480e5b9937~tplv-k3u1fbpfcp-zoom-1.image)](https://jitpack.io/#ChinaLike/ActivityResultApi)\n\n```\ndependencies {\n        implementation 'com.github.ChinaLike:ActivityResultApi:最新版本号'\n        //根据自己版本选择即可，最低不能低于activity-ktx:1.2.3、fragment-ktx:1.3.4\n        implementation(\"androidx.activity:activity-ktx:1.2.3\")\n        implementation(\"androidx.fragment:fragment-ktx:1.3.4\")\n        //ARouter，根据自己需要引入，版本按照自己需要引入\n        //implementation 'com.alibaba:arouter-api:1.5.2'\n        //kapt 'com.alibaba:arouter-compiler:1.5.2'\n}\n```\n\n\u003e step 3.在项目自定义`Application`中添加或在已经有的自定义`Application`的`onCreate()`中添加：\n\n```\nclass BaseApplication:Application() {\n\n    override fun onCreate() {\n        super.onCreate()\n        ...\n        ActivityResultApi.init(this)\n        ...\n    }\n}\n```\n并在`AndroidManifest.xml`中添加\n\n```\n    \u003capplication\n        ...\n        android:name=\".BaseApplication\"\n        ...\u003e\n            ...\n    \u003c/application\u003e\n```\n\n# API介绍\n\n+ 此SDK支持不回调，当不需要回调的时候，使用我们平时的`startActivity()`也是一样的，按照自己习惯选择即可\n+ 当没有Java示例的地方，说明不支持或者使用Java非常麻烦\n\n### registerForActivityResult(intent,activityResultCallback)\n\n#### 参数说明\n\n+ intent：类型Intent，必传\n+ activityResultCallback：类型ActivityResultCallback\u003cActivityResult\u003e，可选，当需要回调时传递，当不需要回调时，可不传递\n\n#### Kotlin示例\n\n+ 带返回\n\n```\nval intent = Intent(this, SecondActivity::class.java)\nintent.putExtra(Key.SOURCE, source)\nregisterForActivityResult(intent) {\n   //回调\n}\n```\n\n+ 不带返回\n\n```\nval intent = Intent(this, SecondActivity::class.java)\nintent.putExtra(Key.SOURCE, source)\nregisterForActivityResult(intent)\n//startActivity(intent) 也一样\n```\n\n#### Java示例\n\n+ 带返回\n\n```\nIntent intent = new Intent(this, SecondActivity.class);\nintent.putExtra(Key.SOURCE, source);\nActivityResultApiExKt.registerForActivityResult(this, intent, result -\u003e \n      //回调\n);\n```\n\n+ 不带返回\n\n```\nIntent intent = new Intent(this, SecondActivity.class);\nintent.putExtra(Key.SOURCE, source);\nActivityResultApiExKt.registerForActivityResult(this, intent);\n```\n\n### \u003cT:FragmentActivity\u003e registerForActivityResult(intentExtra,activityResultCallback)\n\n#### 参数说明\n\n+ intentExtra：类型(intent: Intent) -\u003e Unit，可选\n+ activityResultCallback：类型ActivityResultCallback\u003cActivityResult\u003e，可选，当需要回调时传递，当不需要回调时，可不传递\n\n#### Kotlin示例\n\n+ 不传参，有回调\n\n```\nregisterForActivityResult\u003cSecondActivity\u003e {\n   //回调\n}\n```\n\n+ 传参，有回调\n\n```\nregisterForActivityResult\u003cSecondActivity\u003e({\n   it.putExtra(Key.SOURCE, source)\n}) {\n   //回调\n}\n```\n\n+ 传参，没有回调\n\n```\nregisterForActivityResult\u003cSecondActivity\u003e({\n   it.putExtra(Key.SOURCE, source)\n})\n```\n\n+ 不传参，没有回调\n\n```\nregisterForActivityResult\u003cSecondActivity\u003e(）\n```\n\n### ARouter中，navigation(context,navigationCallback,activityResultCallback)\n\n#### 参数说明\n\n+ context：类型FragmentActivity或Fragment，必传\n+ navigationCallback：类型NavigationCallback，界面监听，可选\n+ activityResultCallback：类型ActivityResultCallback\u003cActivityResult\u003e，回调，必传\n\n#### Kotlin示例\n\n+ 带界面监听和回调\n\n```\nARouter.getInstance()\n    .build(Router.SECOND_ACTIVITY)\n    .withString(Key.SOURCE, source)\n    .navigation(this, object : NavigationCallback {\n\n        override fun onFound(postcard: Postcard?) {\n            \n        }\n\n        override fun onLost(postcard: Postcard?) {\n\n        }\n\n        override fun onArrival(postcard: Postcard?) {\n\n        }\n\n        override fun onInterrupt(postcard: Postcard?) {\n\n        }\n\n    }) {\n   //回调\n    }\n```\n\n+ 不带界面监听但有回调\n\n```\nARouter.getInstance()\n    .build(Router.SECOND_ACTIVITY)\n    .withString(Key.SOURCE, source)\n    .navigation(this) {\n   //回调\n    }\n```\n\n#### Java示例\n\n+ 带界面监听和回调\n\n```\nPostcard postcard = ARouter.getInstance()\n    .build(Router.SECOND_ACTIVITY)\n    .withString(Key.SOURCE, source);\nActivityResultApiExKt.navigation(postcard, this, new NavigationCallback() {\n    @Override\n    public void onFound(Postcard postcard) {\n        \n    }\n\n    @Override\n    public void onLost(Postcard postcard) {\n\n    }\n\n    @Override\n    public void onArrival(Postcard postcard) {\n\n    }\n\n    @Override\n    public void onInterrupt(Postcard postcard) {\n\n    }\n}, new ActivityResultCallback\u003cActivityResult\u003e() {\n\n    @Override\n    public void onActivityResult(ActivityResult result) {\n        //回调\n    }\n});\n```\n\n+ 不带界面监听但有回调\n\n```\nPostcard postcard = ARouter.getInstance()\n        .build(Router.SECOND_ACTIVITY)\n        .withString(Key.SOURCE, source);\nActivityResultApiExKt.navigation(postcard, this, new ActivityResultCallback\u003cActivityResult\u003e() {\n\n    @Override\n    public void onActivityResult(ActivityResult result) {\n        //回调\n    }\n});\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinalike%2Factivityresultapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchinalike%2Factivityresultapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinalike%2Factivityresultapi/lists"}