Managing startActivityForResult on Android

Managing startActivityForResult on Android

6 June 2024 Stephan Petzl Leave a comment Tech-Help

Handling the communication between activities in Android can be a common requirement. One effective method for achieving this is through the startActivityForResult approach. This allows one activity to start another and receive a result once the second activity finishes. However, with the recent updates in Android, this method has been deprecated in favor of a more streamlined approach. This guide will walk you through both the traditional and the new recommended methods.

Traditional Method: Using startActivityForResult

Step 1: Start the Second Activity

From your FirstActivity, call the SecondActivity using the startActivityForResult method:

int LAUNCH_SECOND_ACTIVITY = 1;
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);

Step 2: Set the Result in the Second Activity

In your SecondActivity, set the data which you want to return back to FirstActivity. If you don’t want to return any data, set a canceled result:

Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(Activity.RESULT_OK, returnIntent);
finish();

If you don’t want to return data:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

Step 3: Handle the Result in the First Activity

In your FirstActivity class, override the onActivityResult method to handle the result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == LAUNCH_SECOND_ACTIVITY) {
        if (resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            // Handle the result
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            // Handle the case where there's no result
        }
    }
}

New Method: Using ActivityResultLauncher

With AndroidX, the startActivityForResult method has been deprecated. The new recommended approach is using ActivityResultLauncher and ActivityResultContracts.

Step 1: Register the Activity Result Launcher

Register the launcher in your activity:

ActivityResultLauncher launchSomeActivity = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResultCallback() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent data = result.getData();
                // Handle the result
            }
        }
    }
);

Step 2: Launch the Second Activity

Use the launcher to start the second activity:

public void openYourActivity() {
    Intent intent = new Intent(this, SecondActivity.class);
    launchSomeActivity.launch(intent);
}

Step 3: Handle the Result

The result handling is done within the onActivityResult method as defined in the launcher registration.

Advantages of the New Approach

  • Reduces complexity, especially when calling activities from fragments.
  • Provides a more streamlined and type-safe way to handle results.
  • Makes it easier to request permissions and handle callbacks.

Conclusion

Both methods allow for effective communication between activities. While the traditional startActivityForResult method is still widely used, transitioning to the new ActivityResultLauncher approach provides a more modern and efficient way to handle activity results.

For mobile developers looking to streamline their testing processes, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that simplifies the creation, running, and maintenance of automated tests. It leverages computer vision and AI to ensure fast and reliable testing, freeing developers to focus on creating great products. Whether you’re a developer or a QA, Repeato can significantly enhance your testing workflow.

For more information and detailed documentation, visit our documentation page.

Like this article? there’s more where that came from!