Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tranb3r opened this issue Nov 10, 2022 · 8 comments · Fixed by #11530
Closed
Labels
area-controls-modal area-controls-tabbedpage TabbedPage fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! i/regression This issue described a confirmed regression on a currently supported version platform/android 🤖 t/bug Something isn't working
Milestone

Comments

@tranb3r
Copy link

tranb3r commented Nov 10, 2022

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 xamarin/Xamarin.Forms#1378.
Maybe you should simply change from Commit() to CommitAllowingStateLoss(), as done here: xamarin/Xamarin.Forms#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
@tranb3r tranb3r added the t/bug Something isn't working label Nov 10, 2022
@PureWeen PureWeen added area-controls-tabbedpage TabbedPage i/regression This issue described a confirmed regression on a currently supported version labels Nov 10, 2022
@PureWeen PureWeen added this to the Backlog milestone Nov 10, 2022
@ghost
Copy link

ghost commented Nov 10, 2022

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@tranb3r
Copy link
Author

tranb3r commented Nov 29, 2022

Hi,
This issue is a blocker for the migration of my xamarin.forms app to maui.
Is it going to be fixed soon ? How can I help ?

@jfversluis
Gerald, you've helped a lot with prioritization of xamarin.forms issues, could you please help me here ?
Thank you

@tranb3r
Copy link
Author

tranb3r commented Dec 5, 2022

Here is a repro if you need it.

@tranb3r
Copy link
Author

tranb3r commented Dec 11, 2022

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

@shadowfoxish
Copy link

shadowfoxish commented Feb 3, 2023

I encountered this error in my application as well. Different trigger.

In my case, I have a timer thread running which periodically wakes up, checks if the user has made any recent activity. If after a certain amount of time passes and there is no activity, the thread logs the user out of their session and then replaces the MainPage with a new instance of the login screen.

During this period, the device may have gone to sleep and my application is not currently displayed.

Thread started: <Thread Pool> #208
[AndroidKeysetManager] keyset not found, will generate a new one. can't read keyset; the pref value __androidx_security_crypto_encrypted_prefs_key_keyset__ does not exist
[AndroidKeysetManager] keyset not found, will generate a new one. can't read keyset; the pref value __androidx_security_crypto_encrypted_prefs_value_keyset__ does not exist
[0:] Error in session manager: 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.Platform.NavigationRootManager.SetContentView(IView view) in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\NavigationRootManager.cs:line 147
   at Microsoft.Maui.Platform.NavigationRootManager.Connect(IView view, IMauiContext mauiContext) in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\NavigationRootManager.cs:line 88
   at Microsoft.Maui.Handlers.WindowHandler.CreateRootViewFromContent(IWindowHandler handler, IWindow window) in D:\a\_work\1\s\src\Core\src\Handlers\Window\WindowHandler.Android.cs:line 79
   at Microsoft.Maui.Handlers.WindowHandler.MapContent(IWindowHandler handler, IWindow window) in D:\a\_work\1\s\src\Core\src\Handlers\Window\WindowHandler.Android.cs:line 24
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IWindow, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IWindowHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172
   at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47
   at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 72
   at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 87
   at Microsoft.Maui.Controls.Window.OnPropertyChanged(String propertyName) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Window\Window.Impl.cs:line 253
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 533
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 469
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 391
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 365
   at Microsoft.Maui.Controls.Window.set_Page(Page value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Window\Window.Impl.cs:line 113
   at Microsoft.Maui.Controls.Application.set_MainPage(Page value) in D:\a\_work\1\s\src\Controls\src\Core\Application.cs:line 107
   at InventoryFoxApp.Pages.App.ResetForLogin() in C:\Code\AirWOLF\InventoryFoxApp\InventoryFoxApp\Pages\LoginAndRoot\App.xaml.cs:line 163
   at InventoryFoxApp.Pages.App.Logout() in C:\Code\AirWOLF\InventoryFoxApp\InventoryFoxApp\Pages\LoginAndRoot\App.xaml.cs:line 201
   at InventoryFoxApp.Pages.App.<T_Elapsed>b__8_0() in C:\Code\AirWOLF\InventoryFoxApp\InventoryFoxApp\Pages\LoginAndRoot\App.xaml.cs:line 129
  --- End of managed Java.Lang.IllegalStateException stack trace ---
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
	at androidx.fragment.app.FragmentManager.checkStateLoss(FragmentManager.java:1610)
	at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1650)
	at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:341)
	at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:306)
	at mono.java.lang.RunnableImplementor.n_run(Native Method)
	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:31)
	at android.os.Handler.handleCallback(Handler.java:873)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:193)
	at android.app.ActivityThread.main(ActivityThread.java:6702)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

  --- End of managed Java.Lang.IllegalStateException stack trace ---
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
	at androidx.fragment.app.FragmentManager.checkStateLoss(FragmentManager.java:1610)
	at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1650)
	at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:341)
	at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:306)
	at mono.java.lang.RunnableImplementor.n_run(Native Method)
	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:31)
	at android.os.Handler.handleCallback(Handler.java:873)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:193)
	at android.app.ActivityThread.main(ActivityThread.java:6702)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
Thread finished: <Thread Pool> #205

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.

@PureWeen
Copy link
Member

PureWeen commented 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

@shadowfoxish
Copy link

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.

@tranb3r
Copy link
Author

tranb3r commented Feb 7, 2023

@PureWeen
I've tested your workaround (https://github.com/PureWeen/ShanedlerSamples), but the exception is still happening.
Could you please confirm this issue is really fixed by #11530 ?
Thanks.

@samhouts samhouts added the fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! label Feb 22, 2023
@samhouts samhouts modified the milestones: Backlog, 8.0-preview1 Feb 22, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-modal area-controls-tabbedpage TabbedPage fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! i/regression This issue described a confirmed regression on a currently supported version platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants