Xamarin Android中如何设置自定义Activity结果码?
Great question! I've run into this exact issue before when working with Xamarin.Android, and there are two reliable workarounds to bypass the Result enum restriction while still adhering to Android's official guidelines for custom result codes.
1. Cast Custom Integer Values to the Result Enum
Since the Android.App.Result enum is just a wrapper around integer values under the hood, you can safely cast your custom result code (as long as it's greater than Result.FirstUser, which equals 1) to the Result type when calling SetResult(). Then, cast it back to an integer in OnActivityResult() to check your custom value.
Example: Setting the Custom Result Code
// In the child Activity where you want to return a custom result const int CustomResultSuccess = 1001; // Greater than Result.FirstUser (1) var resultIntent = new Intent(); // Add any data you need to pass back SetResult((Result)CustomResultSuccess, resultIntent); Finish();
Example: Handling the Custom Result in the Parent Activity
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); int customResultCode = (int)resultCode; if (requestCode == YourRequestCode) { if (customResultCode == 1001) { // Handle your custom success result } else if (customResultCode == 1002) { // Handle another custom result } else if (resultCode == Result.Ok) { // Handle default OK result } } }
This approach works because Android's documentation explicitly states values above FirstUser are safe for custom use, and Xamarin's enum casting doesn't break the underlying Android framework behavior.
2. Pass Custom Result Codes via Intent Extras
If you prefer to avoid enum casting entirely, you can store your custom result code as an extra in the Intent you pass back. This is a more explicit approach and avoids any potential edge cases with enum type safety.
Example: Setting the Custom Result via Extra
// In the child Activity const int CustomResultWarning = 2001; var resultIntent = new Intent(); resultIntent.PutExtra("CustomResultCode", CustomResultWarning); // You can still set a base Result enum value if needed, e.g., Result.Ok SetResult(Result.Ok, resultIntent); Finish();
Example: Retrieving the Custom Result in the Parent Activity
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (requestCode == YourRequestCode && data != null) { int customResult = data.GetIntExtra("CustomResultCode", (int)Result.Canceled); switch (customResult) { case 2001: // Handle custom warning result break; case 2002: // Handle another custom result break; default: // Handle default result codes break; } } }
This method is especially useful if you need to return multiple custom states without worrying about enum value collisions.
Both approaches are valid and align with Android's best practices. The first is more concise if you only need a few custom codes, while the second is more flexible for complex scenarios.
内容的提问来源于stack exchange,提问作者Sam Liddle

