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

Xamarin中SecondActivity的OnActivityResult未触发问题求助

Hey there, let's figure out why your SecondActivity's OnActivityResult method isn't getting triggered after returning from ThirdActivity. Based on your code snippets and the extra details you provided, here are the key troubleshooting steps and fixes:

Common Causes & Solutions

1. Check SecondActivity's Launch Mode (Most Likely Culprit!)

If your SecondActivity is using a special launch mode like singleTask or singleInstance, Android will place it in a separate task stack. This breaks the StartActivityForResult callback chain because the result can't be sent across task stacks.

Double-check your SecondActivity attribute—if you've set a custom launch mode, switch back to the default Standard:

// ❌ Avoid this if you're using StartActivityForResult
[Activity(Label = "SecondActivity", LaunchMode = LaunchMode.SingleTask)]

// ✅ Use the default (you don't even need to explicitly write LaunchMode.Standard)
[Activity(Label = "SecondActivity")]

2. Verify ThirdActivity's Launch Mode

Same rule applies to ThirdActivity—if it's set to singleTask or singleInstance, it won't return properly to SecondActivity's task stack. Ensure it uses the default Standard launch mode too.

3. Make Sure You're Not Breaking the OnActivityResult Flow

Your code includes base.OnActivityResult(requestCode, resultCode, data); which is correct, but double-check that there's no early return or logic that cuts off the method before it can process the result. For example:

protected override void OnActivityResult(int requestCode, Android.App.Result resultCode, Intent data)
{
    // ❌ Don't add an early return here
    // return;

    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == EDIT_ACTIVITY)
    {
        if (resultCode == Result.Ok)
        {
            RunOnUiThread(() => PopulateScreen());
        }
    }
}

4. Confirm FinishSuccess() Is Actually Executing

Add a debug log to your ThirdActivity's FinishSuccess() method to make sure it's being called when the edit completes. If this method never runs, no result will be sent back:

private void FinishSuccess()
{
    // Add this to verify execution
    Android.Util.Log.Debug("ThirdActivity", "FinishSuccess triggered - setting result to Ok");
    SetResult(Result.Ok, new Intent());
    Finish();
}

5. Check for Activity Recreation

If SecondActivity gets killed by the system due to low memory while ThirdActivity is active, Android will rebuild SecondActivity when you return. In this case, OnActivityResult will still fire, but you might need to restore any saved state in OnCreate (using savedInstanceState) to properly refresh your list.

Quick Test to Isolate the Issue

To narrow down the problem, create a simple test Activity (let's call it TestActivity) with just a button that calls SetResult(Result.Ok) and Finish(). Launch it from SecondActivity using StartActivityForResult(intent, TEST_REQUEST_CODE).

  • If OnActivityResult fires for TestActivity, the issue is specific to your ThirdActivity's logic or configuration.
  • If it still doesn't fire, the problem is definitely in SecondActivity's launch mode or setup.

内容的提问来源于stack exchange,提问作者asapoverflow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:55:13