Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
See original GitHub issueDescription
Exception on app resume on android, when pushing a modal page. No exception on a similar scenario with xamarin forms.
This looks similar to https://github.com/xamarin/Xamarin.Forms/issues/1378.
Maybe you should simply change from Commit() to CommitAllowingStateLoss(), as done here: https://github.com/xamarin/Xamarin.Forms/pull/527.
Steps to Reproduce
- Create a maui application for android
- Make the mainpage either a TabbedPage or a ContentPage
- In application OnResume, push a modal page (call PushModalAsync)
- Launch app, move app to background, then resume app. There is an exception and the modal page is not displayed.
Link to public reproduction project repository
…
Version with bug
6.0.486 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 13
Did you find any workaround?
No response
Relevant log output
Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at Java.Interop.JniEnvironment.InstanceMethods.CallIntMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 11725
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeAbstractInt32Method(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 492
at AndroidX.Fragment.App.BackStackRecord.Commit() in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.BackStackRecord.cs:line 131
at Microsoft.Maui.Controls.Handlers.TabbedPageManager.RemoveTabs() in D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\TabbedPageManager.cs:line 186
at Microsoft.Maui.Controls.Handlers.TabbedPageManager.OnTabbedPageDisappearing(Object sender, EventArgs e) in D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\TabbedPageManager.cs:line 201
at Microsoft.Maui.Controls.Page.SendDisappearing() in D:\a\_work\1\s\src\Controls\src\Core\Page.cs:line 520
at Microsoft.Maui.Controls.Internals.NavigationModel.PushModal(Page page) in D:\a\_work\1\s\src\Controls\src\Core\NavigationModel.cs:line 204
at Microsoft.Maui.Controls.Platform.ModalNavigationManager.PushModalAsync(Page modal, Boolean animated) in D:\a\_work\1\s\src\Controls\src\Core\Platform\ModalNavigationManager\ModalNavigationManager.Android.cs:line 117
at Microsoft.Maui.Controls.Window.NavigationImpl.OnPushModal(Page modal, Boolean animated) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Window\Window.Impl.cs:line 714
Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at Java.Interop.JniEnvironment.InstanceMethods.CallIntMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 11725
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeAbstractInt32Method(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 492
at AndroidX.Fragment.App.BackStackRecord.Commit() in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.BackStackRecord.cs:line 131
at Microsoft.Maui.Controls.Platform.ModalNavigationManager.ModalContainer..ctor(IMauiContext windowMauiContext, Page modal, ViewGroup parentView) in D:\a\_work\1\s\src\Controls\src\Core\Platform\ModalNavigationManager\ModalNavigationManager.Android.cs:line 228
at Microsoft.Maui.Controls.Platform.ModalNavigationManager.PresentModal(Page modal, Boolean animated) in D:\a\_work\1\s\src\Controls\src\Core\Platform\ModalNavigationManager\ModalNavigationManager.Android.cs:line 130
at Microsoft.Maui.Controls.Platform.ModalNavigationManager.PushModalAsync(Page modal, Boolean animated) in D:\a\_work\1\s\src\Controls\src\Core\Platform\ModalNavigationManager\ModalNavigationManager.Android.cs:line 119
at Microsoft.Maui.Controls.Window.NavigationImpl.OnPushModal(Page modal, Boolean animated) in
Issue Analytics
- State:
- Created 10 months ago
- Reactions:2
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Can not perform this action after onSaveInstanceState with ...
Here's what I did to solve it: As it is not possible to avoid the IllegalStateException in the callback, catch & ignore it....
Read more >Can not perform this action after onSaveInstanceState ...
Handling IllegalStateException: Can not perform this action after onSaveInstanceState. Have you ever came across this error? If you have not seen it yourselves ......
Read more >Avoid Fragment IllegalStateException: Can not perform this ...
I've been recently working on an app that required the use of different Fragments depending on the response that we get from the...
Read more >IllegalStateException : Cannot perform this action after ...
The main root cause of this exception is that fragment transactions are not allowed after the OnSaveInstanceState of the activity is called.
Read more >Bug: "IllegalStateException: Can not perform this action after ...
It crashes because of using fragments, which do not accept being added&committed in specific activity lifecycle times. Since those bugs are so old,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Nice, thanks! That might be simpler than what I ended up implementing. I used App.xaml.cs’s OnResume and OnSleep methods, and just wrote a bool flag into my session manager object which I used to determine app state.
Workaround: add a short delay (1ms is enough) before pushing the modal page. Thanks @PureWeen for helping !