RegisterForActivityResult does not work
Description
RegisterForActivityResult does not work correctly with MauiAppCompatActivity.
I think the issue lies in these two lines:
savedInstanceState?.Remove("android:support:fragments");
savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
Steps to Reproduce
- Clone the repository
- Open
AndroidApp1.sln - Enable
Don't keep activitieson Device\Simulator - Add breakpoints in two projects in methods
OnActivityResult - Run
AndroidApp1 - Touch
Get result - Choose a contact
- Run
MauiApp1 - Touch
Get result - Choose a contact
Link to public reproduction project repository
https://github.com/dimonovdd/RegisterForActivityResult.MAUI
Version with bug
8.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Any
Did you find any workaround?
No response
Relevant log output
No response
@PureWeen Hi
Do you have any ideas? These lines have been changed in #1241
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.
Backlog? @jsuarezruiz Do you understand the seriousness of this problem? Are we following the path of Xamarin.Froms? If an issue is difficult, then you simply do not have the desire to solve it for several years?
@jonathanpeppers Hi, You are strong in Android development. Do you have any ideas? How can we disable fragment restore without disabling restore of other things?
@dimonovdd it looks like you are supposed to override:
protected virtual bool AllowFragmentRestore => false;
Did that not work for you?
@PureWeen can maybe share details on why we need:
savedInstanceState?.Remove("android:support:fragments");
savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
@dimonovdd it looks like you are supposed to override:
I didn't understand the meaning of it https://github.com/dotnet/maui/discussions/14039
I think you override it and return true, will prevent savedInstanceState from being modified?
will prevent savedInstanceState from being modified?
Yes, but the app will crash. And there is no documentation of what else needs to be changed.
What is the error message?
A screenshot was attached in the discussion.
This exception is not only related to ShellItemRenderer. Almost nothing has ctor (IntPtr javaReference, JniHandleOwnership transfer) or empty ctor

MAUI changed the logic of restoring activity after destroy. These two lines disable default restore of fragments and other Views. But it also disables state restore of other things (RegisterForActivityResult and etc.)
savedInstanceState?.Remove("android:support:fragments");
savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
@dimonovdd it looks like you are supposed to override:
protected virtual bool AllowFragmentRestore => false;Did that not work for you?
@PureWeen can maybe share details on why we need:
savedInstanceState?.Remove("android:support:fragments"); savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
The top one has been around forever in Forms https://github.com/xamarin/Xamarin.Forms/pull/246
The second one we added when we bumped up AndroidX because it was causing a similar issue indicated by https://github.com/xamarin/Xamarin.Forms/pull/246
https://github.com/xamarin/Xamarin.Forms/pull/14101/files#diff-89f66fc6d9b2094dcce8863bd3db93823b5ca20d2f2ef588b8e6ffff46fcb0b6R226
Do you happen to know which of these lines is breaking RegisterForActivityResult?
We should probably re-evaluate these. They feel like something that's just adding a band-aid to a different problem.
Do you happen to know which of these lines is breaking RegisterForActivityResult?
yep, savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
Do you happen to know which of these lines is breaking RegisterForActivityResult?
yep,
savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
If you set AllowFragmentRestore to true and then just add
savedInstanceState?.Remove("android:support:fragments");
Does that workaround your issue?
Does that workaround your issue?
No, I changed MauiAppCompatActivity code like this and also the exception

Does that workaround your issue?
No, I changed
MauiAppCompatActivitycode like this and also the exception
Hmm, yea, it looks like that path broke when we updated AndroidX and then needed to add that other line to account for it.
Hmm, yea, it looks like that path broke when we updated AndroidX and then needed to add that other line to account for it.
I think it will be very painful to tear off this patch. But we definitely have to fix it
Is that issue still in backlog? In how many years will we be able to see a solution?
Verified this issue with Visual Studio Enterprise 17.8.0 Preview 1.0. Can repro on android platform with sample project.
https://github.com/dimonovdd/RegisterForActivityResult.MAUI
~~The issue in the sample is probably in creating ActivityResultLauncher before base.OnCreate: https://github.com/dimonovdd/RegisterForActivityResult.MAUI/blob/main/MauiApp1/Platforms/Android/MainActivity.cs#L18
Not in MAUI.~~
Seems like it doesn't matter where it's registered. So @dimonovdd is right that removing the lifecycle saved state doesn't let Activity Result Registry restore and apply pending results.
I'm having success with this workaround:
public class MainActivity : MauiAppCompatActivity
{
protected override bool AllowFragmentRestore => true;
protected override void OnCreate(Bundle? savedInstanceState)
{
...
savedInstanceState?
.GetBundle("androidx.lifecycle.BundlableSavedStateRegistry.key")?
.Remove("android:support:fragments");
base.OnCreate(savedInstanceState);
}
}
This should probably be the fix that goes in MauiAppCompatActivity but I'm having trouble proving it in their unit/device/ui test projects.
Basically, I've found that android:support:fragments is no longer a direct child of savedInstanceState (probably an AndroidX change somewhere) but rather a child of the androidx.lifecycle.BundlableSavedStateRegistry.key bundle. Thus, savedInstanceState?.Remove("android:support:fragments"); doesn't appear to have any affect and savedInstanceState?.Remove("androidx.lifecycle.BundlableSavedStateRegistry.key"); wipes out both the fragment bundle and the ActivityResultRegistry required by the new Activity Result API's.