OnActivityResult()与OnNewIntent()的区别及自定义调用场景咨询
Hey there! Let's tackle your questions one by one, nice and clear.
1. What's the difference between
onActivityResult() and onNewIntent()? These two methods serve completely different purposes in Android Activity lifecycle:
onActivityResult(): This is the go-to method for handling results from a child Activity that you started withstartActivityForResult()(or the newer AndroidXActivityResultContracts). It’s built for a request-response pattern—you explicitly launch a child Activity expecting data back, and when the child callssetResult()and finishes, this method fires in the parent to process the returned data.onNewIntent(): This method kicks in when you’re reusing an existing Activity instance (thanks to launch modes likesingleTop,singleTask, orsingleInstance). Instead of creating a new Activity instance when it’s requested again, the system passes the new Intent to the existing instance via this method. It’s all about reusing Activity instances, not handling results from a child that’s finishing.
2. How to implement your desired behavior
You want onActivityResult() to trigger when returning from MenuActivity, and onNewIntent() for other child pages. Here’s how to make that happen:
For MenuActivity (trigger onActivityResult())
We’ll use the standard result-based launch flow:
- Launch
MenuActivitywith a result request:
If you’re using the older API:
For AndroidX (recommended, as// In MainActivity private static final int REQUEST_CODE_MENU = 1001; // To launch MenuActivity Intent menuIntent = new Intent(this, MenuActivity.class); startActivityForResult(menuIntent, REQUEST_CODE_MENU);startActivityForResult()is deprecated):// In MainActivity private ActivityResultLauncher<Intent> menuLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { // Handle the result from MenuActivity here (equivalent to onActivityResult logic) Intent data = result.getData(); if (data != null) { // Extract any passed data } } } ); // To launch MenuActivity menuLauncher.launch(new Intent(this, MenuActivity.class)); - Return a result from
MenuActivity:
When the user clicks the back button inMenuActivity, send a result before finishing:
This will trigger// In MenuActivity's back button click listener backButton.setOnClickListener(v -> { // Optional: Pass data back to MainActivity Intent resultIntent = new Intent(); resultIntent.putExtra("menu_selected", "some_value"); setResult(RESULT_OK, resultIntent); finish(); });onActivityResult()(or the AndroidX callback) in MainActivity.
For other child pages (trigger onNewIntent())
We’ll use Activity launch mode to reuse the existing MainActivity instance:
- Set MainActivity’s launch mode to
singleTop:
Update yourAndroidManifest.xml:<activity android:name=".MainActivity" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>singleTopmeans if MainActivity is already at the top of the task stack, a new launch request will reuse the existing instance instead of creating a new one. - Launch MainActivity from other child pages:
Instead of just finishing the child Activity, explicitly launch MainActivity with a new Intent:// In OtherActivity's back button click listener backButton.setOnClickListener(v -> { Intent mainIntent = new Intent(this, MainActivity.class); // Pass data to MainActivity if needed mainIntent.putExtra("from_child", "OtherActivity"); startActivity(mainIntent); finish(); // Close the child Activity }); - Handle the new Intent in MainActivity:
OverrideonNewIntent()to process the incoming Intent:// In MainActivity @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // Update the Activity's current Intent (important for future references) setIntent(intent); // Handle the data from the child page String fromChild = intent.getStringExtra("from_child"); if ("OtherActivity".equals(fromChild)) { // Execute your desired logic here } }
This setup ensures that returning from MenuActivity triggers the result flow, while returning from other child pages reuses the MainActivity instance and fires onNewIntent().
内容的提问来源于stack exchange,提问作者Jung Jaehoon
相关产品推荐
相关产品推荐

