Sometimes, we want some pages bring some data after closing it, e.g., returning the phone number of someone.

The mainstream way is to use Activity Result API. However, in some old tutorials, startActivityForResult, onActivityResult() might be used.

Activity Result API

Activity result API is introduced to simply the procedure of returning results and decouple components. The core includes:

  • ActivityResultContract

    which defines how to create Intent and how to parse the returned result Intent.

  • ActivityResultLauncher

    We should first call registerForActivityResult() to register a contract and a callback, to get this launcher. Then invoking the launch() method of this launcher to start target activity.

  • ActivityResultCallback.

    When the target activity returns, this callback will be triggered, which can be exploited to handle returned data.

Concrete Steps

  1. Register at source activity.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 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);
});
}
}
  1. Process data and return at target activity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

editText = findViewById(R.id.editTextForReturn);
Button returnButton = findViewById(R.id.returnButton);

// retrieve data passed from source activity
String greeting = getIntent().getStringExtra("GREETING");

returnButton.setOnClickListener(v -> {
String input = editText.getText().toString();

// a. create an empty Intent to store data for return
Intent returnIntent = new Intent();

// b. put data into intent
returnIntent.putExtra("RETURN_DATA", input);

// c. set result_code and returned intent
setResult(Activity.RESULT_OK, returnIntent);

// d. close current Activity
finish();
});
}
}