Retrieving Results from Activities

Table of Contents

1. Activity Result APIs

The standard way to retrieve returned result is through Activity Result API. It’s introduced to simplify the procedure of returning results and decouple components. The core includes

ActivityResultContract
which defines how to create an Intent and how to parse the returned Intent
ActivityResultCallback
when the target activity returns, this callback will be triggered, which can be used to process returned data
ActivityResultLauncher
We should first call registerForActivityResult() to register a contract and a callback to get this launcher. Then, we just need to invoke its launch() method to automatically start target activity and process returned data.

1.1. Implementation Detail

  1. Register at source activity.

    This registration must be done at onCreate(), or member declaration.

         // MainActivity.java
         public class MainActivity extends AppCompatActivity {
             private TextView resultTextView;
    
             // 1. Register ActivityResultLauncher
             private final ActivityResultLauncher<Intent>
                         secondActivityLauncher = registerForActivityResult(
                                 // use a preset Contract
                                 new ActivityResultContracts.StartActivityForResult(),
    
                                 // set the callback handler
                     new ActivityResultCallback<ActivityResult>() {
                         @Override
                         public void onActivityResult(ActivityResult result) {
                             // 4. process returned result in callback.
                             if (result.getResultCode() == Activity.RESULT_OK) {
                                 Intent data = result.getData();
                                 if (data != null) {
                                     String returnedData = data.getStringExtra("RETURN_DATA");
                                     resultTextView.setText("Returned from SecondActivity: " + returnedData);
                                 }
                             }
                         }
                     });
    
             @Override
             protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_main);
    
                 resultTextView = findViewById(R.id.resultTextView);
                 Button launchButton = findViewById(R.id.launchButtonForReuslt);
    
                 launchButton.setOnClickListener(v -> {
                     // 2. create Intent
                     Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                     intent.putExtra("GREETING", "Hello from MainActivity!");
    
                     // 3. use launcher to start Activity
                     secondActivityLauncher.launch(intent);
                 });
             }
         }
    
  2. Process data and return at target activity

Date: 2026-06-02 Tue