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:
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.
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
OnActivityResultfires forTestActivity, the issue is specific to yourThirdActivity's logic or configuration. - If it still doesn't fire, the problem is definitely in
SecondActivity's launch mode or setup.
内容的提问来源于stack exchange,提问作者asapoverflow

