onActivityResult
@SuppressWarnings("deprecation") @Override @CallSuper protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { (); (requestCode, resultCode, data); }
Use of Activity Results API
In the latest beta version, Google recommends using the Activity Results API to handle page data processing. Through the new Activity Result API, we can handle result callbacks in separate classes to truly achieve a single responsibility.
1. Add dependencies to the app:
implementation ':activity-ktx:1.2.0-beta01'
implementation ':fragment-ktx:1.3.0-beta01'
2. Customize ActivityResultContract
Custom ActivityResultContract, used to handle the processing of data passing between two activities, instead of the onActivityResult method. The first generic parameter of ActivityResultContract<String, String> represents the input parameter type, that is, the data type to be brought to the next Activity, and the second generic parameter represents the output parameter type, that is, the data type of the second Activity passes back to the first Activity.
class CustomActivityResultContract : ActivityResultContract<String, String>() { override fun createIntent(context: Context, input: String?): Intent { //To pass data to the next activity, bring data from A activity to B activity. return Intent(context, SecondActivity::).putExtra("name", input) } override fun parseResult(resultCode: Int, intent: Intent?): String { //Data returned from activity val result = intent?.getStringExtra("result") //The data returned from B activity. if (resultCode == Activity.RESULT_OK && result != null) { return "$result" } return "" } }
3. Register the ActivityResult protocol
Register the ActivityResult protocol in A activity, using the CustomActivityResultContract we just defined.
registerForActivityResult method has two parameters. The first parameter is to pass the corresponding Contract, and the second parameter is to callback result callback.
private val activityLauncher = registerForActivityResult(CustomActivityResultContract()) { //Callback method to return to the first page after the second page is closed = it }
4. Call launch method to open the interface jump
Use the launch method of the launcher object generated in the third step to start the interface jump. The launch method needs to pass an input parameter, that is, the parameter that needs to be brought to the next page, which can be any object:
{ //Click to jump to SecondActivity ("I'm the parameter passed by the first page") }
5. Use Google's built-in ActivityResultContract to perform page jumps
You can also call Google's built-in Contract for us through the ActivityResultContract class, which contains various commonly used Contracts.
- @StartActivityForResult: General Contract, no conversion is done, Intent is used as input, ActivityResult is used as output, which is also the most commonly used contract.
- @RequestMultiplePermissions: Used to request a set of permissions
- @RequestPermission: Used to request a single permission
- @TakePicturePreview: Call MediaStore.ACTION_IMAGE_CAPTURE to take a photo, and the return value is Bitmap picture
- @TakePicture: Call MediaStore.ACTION_IMAGE_CAPTURE to take a photo, and save the image to the given Uri address. Return true to indicate that the save is successful.
- @TakeVideo: Call MediaStore.ACTION_VIDEO_CAPTURE to shoot the video, save it to the given Uri address, and return a thumbnail.
- @PickContact: Get contacts from the address book app
- @GetContent: The prompt is to select a content and return a Uri address (content:// form) that accesses native data through ContentResolver#openInputStream(Uri). By default, it adds Intent#CATEGORY_OPENABLE, returning content that can represent the stream.
- @CreateDocument: Prompt the user to select a document and return a Uri starting with (file:/http:/content:).
- @OpenMultipleDocuments: Prompt the user to select documents (multiple can be selected), return their Uri respectively, in the form of List.
- @OpenDocumentTree: Prompt the user to select a directory and return the user's choice as a Uri return. The application can fully manage the documents returned in the directory.
Generally speaking, using StartActivityForResult can meet most needs.
private val activityResultLauncher = registerForActivityResult(()) { if ( == RESULT_OK) { val result = intent?.getStringExtra("result") = result } } { //Click to jump to SecondActivity val intent = Intent(this, SecondActivity::) ("name", "I'm the parameter passed by the first page") (intent) }
Using the built-in ActivityResultContract of the system can easily implement the value between pages, which can also implement the uncoupling operation.
This is the article about the Android Activity Results API processing page data instead of onActivityResult. For more related Android Activity Results API content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!