You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 with startActivityForResult() (or the newer AndroidX ActivityResultContracts). It’s built for a request-response pattern—you explicitly launch a child Activity expecting data back, and when the child calls setResult() 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 like singleTop, singleTask, or singleInstance). 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:

  1. Launch MenuActivity with a result request:
    If you’re using the older API:
    // 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);
    
    For AndroidX (recommended, as 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));
    
  2. Return a result from MenuActivity:
    When the user clicks the back button in MenuActivity, send a result before finishing:
    // 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();
    });
    
    This will trigger 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:

  1. Set MainActivity’s launch mode to singleTop:
    Update your AndroidManifest.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>
    
    singleTop means 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.
  2. 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
    });
    
  3. Handle the new Intent in MainActivity:
    Override onNewIntent() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:35:05