question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState

See original GitHub issue

Description

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

  1. Create a maui application for android
  2. Make the mainpage either a TabbedPage or a ContentPage
  3. In application OnResume, push a modal page (call PushModalAsync)
  4. 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:closed
  • Created 10 months ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
shadowfoxishcommented, Feb 3, 2023

I worked around this using a combination of two people’s solutions/advice. I used a TaskCompletionSource to gate the logout function until the app resumed, if the app wasnt already running, and also added an await Task.Delay(100), which was also necessary.

I think what also might work for you here is to check Window.IsActive. That basically means the app has been backgrounded. If that is false then don’t do any page transitions.

You can add an event to window.resumed/window.activated

once that fires you can then swap out the main page or do whatever it is you need to do

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.

2reactions
tranb3rcommented, Dec 11, 2022

Workaround: add a short delay (1ms is enough) before pushing the modal page. Thanks @PureWeen for helping !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found