当前Activity调用finish()后前序Activity触发方法及RecyclerView更新时机咨询
Question 1: Which lifecycle method does the previous Activity trigger when calling finish() on the current Activity?
When you call finish() on the current Activity (say, Activity B), the system destroys Activity B and brings the prior Activity (Activity A) back to the foreground. The exact lifecycle flow for Activity A depends on its state before Activity B launched:
- If Activity A was stopped (the standard case for full-screen Activities launched over it), it will go through this sequence:
onRestart()→onStart()→onResume() - If Activity A was only paused (e.g., if Activity B was a transparent/dialog-style Activity), it will directly trigger
onResume()
In most everyday scenarios, you can reliably count on onResume() being called on the previous Activity when the current one finishes.
Question 2: Where to place Adapter.notifyDataSetChanged() to update RecyclerView after returning from the second Activity?
You have two solid options, depending on how you launched the second Activity:
Option 1: Use onResume() (simple, universal)
Since onResume() is guaranteed to fire when returning to the first Activity, you can refresh your data source and trigger the adapter update here. Note that onResume() may also run for other reasons (like returning from the home screen), so you might want to add a check to confirm the data actually changed to avoid unnecessary refreshes.
Example code for the first Activity:
@Override protected void onResume() { super.onResume(); // Fetch updated data from your database List<YourDataModel> updatedData = loadUpdatedDataFromDb(); yourRecyclerViewAdapter.setData(updatedData); yourRecyclerViewAdapter.notifyDataSetChanged(); }
Option 2: Use the Activity Result API (targeted, efficient)
If you launched the second Activity with registerForActivityResult() (the modern replacement for startActivityForResult()), you can update the RecyclerView only when the second Activity explicitly confirms a data change. This avoids unnecessary refreshes from unrelated onResume() triggers.
First, register the result listener in the first Activity:
ActivityResultLauncher<Intent> secondActivityLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { // Refresh RecyclerView only when data was updated List<YourDataModel> updatedData = loadUpdatedDataFromDb(); yourRecyclerViewAdapter.setData(updatedData); yourRecyclerViewAdapter.notifyDataSetChanged(); } } ); // Launch the second Activity using the launcher secondActivityLauncher.launch(new Intent(this, SecondActivity.class));
Then, modify your second Activity's onOptionsItemSelected method to signal a successful update before finishing:
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle toolbar back click if (item.getItemId() == android.R.id.home) { // Indicate data was updated to the first Activity setResult(RESULT_OK); finish(); // Close current Activity and return to the list } return super.onOptionsItemSelected(item); }
This approach ensures updates only happen when you know the database was modified, making it more efficient than relying solely on onResume().
内容的提问来源于stack exchange,提问作者earthling

