'startActivityForResult(android.content.Intent, int)' is deprecated
时间: 2024-03-23 08:17:05 浏览: 252
`startActivityForResult(Intent, int)` is indeed deprecated since Android 11 (API level 30). It means that this method will no longer be supported in future versions of Android. Instead, you should use the `registerForActivityResult` method, which is a more modern way to handle activities that return results.
Here's an example of how to use `registerForActivityResult`:
```
val someActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// Handle the result
val data: Intent? = result.data
}
}
// Call the activity with startActivityForResult
val intent = Intent(this, SomeActivity::class.java)
someActivityResultLauncher.launch(intent)
```
With this approach, you create an instance of `ActivityResultLauncher` by calling `registerForActivityResult`, passing in an instance of `ActivityResultContracts.StartActivityForResult`. You then define a lambda that will be called when the activity returns a result. Finally, you call `someActivityResultLauncher.launch(intent)` to start the activity and wait for the result to come back.
Note that `registerForActivityResult` is only available in AndroidX libraries, so if you're not already using them, you'll need to migrate your project to AndroidX before you can use this method.
阅读全文