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 after MainPage swap

See original GitHub issue

Description

Exception Java.Lang.IllegalStateException is thrown when I try to change the Application.Current.MainPage after the browser opened with WebAuthenticator.Default.AuthenticateAsync is closed without any login.

Steps to Reproduce

  • Checkout the attached repo
  • Go to the example OIDC server https://openidconnect.net/ and copy the generated URL
  • Paste the URL into MainPage.xaml.cs line 16
  • Run app
  • Click the “Login” button
  • Close the browser, do not attempt to login at all
  • Exception is thrown on line 25

Link to public reproduction project repository

https://github.com/czmirek/maui_auth_mainpage_fail

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12, API 31, x86_64

Did you find any workaround?

Yes, see this commit in another branch of the repo. I figured out that MainActivity.OnResume must run first before MainPage is swapped. It looks like a race condition. The workaround is simple with TaskCompletionSource but I’m not sure if this is going to work in all cases.

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.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 MauiApp1.MainPage.OnCounterClicked(Object sender, EventArgs e) in C:\Users\lesar\source\repos\maui_auth_mainpage_fail\MauiApp1\MainPage.xaml.cs:line 27  --- 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:1601)	at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1641)	at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:341)	at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:306)	at crc6468b6408a11370c2f.WebAuthenticatorIntermediateActivity.n_onResume(Native Method)	at crc6468b6408a11370c2f.WebAuthenticatorIntermediateActivity.onResume(WebAuthenticatorIntermediateActivity.java:41)	at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1476)	at android.app.Activity.performResume(Activity.java:8191)	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4747)	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4790)	at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)	at android.os.Handler.dispatchMessage(Handler.java:106)	at android.os.Looper.loopOnce(Looper.java:201)	at android.os.Looper.loop(Looper.java:288)	at android.app.ActivityThread.main(ActivityThread.java:7839)	at java.lang.reflect.Method.invoke(Native Method)	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)  --- 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:1601)	at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1641)	at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:341)	at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:306)	at crc6468b6408a11370c2f.WebAuthenticatorIntermediateActivity.n_onResume(Native Method)	at crc6468b6408a11370c2f.WebAuthenticatorIntermediateActivity.onResume(WebAuthenticatorIntermediateActivity.java:41)	at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1476)	at android.app.Activity.performResume(Activity.java:8191)	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4747)	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4790)	at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)	at android.os.Handler.dispatchMessage(Handler.java:106)	at android.os.Looper.loopOnce(Looper.java:201)	at android.os.Looper.loop(Looper.java:288)	at android.app.ActivityThread.main(ActivityThread.java:7839)	at java.lang.reflect.Method.invoke(Native Method)	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)}

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
PureWeencommented, Dec 10, 2022

@czmirek I was able to reproduce your issue. From my reproduction, it seems like the issue happens because the MainPage swap is happening while the app is stopped so your workaround is waiting for the app to re-resume. That’s something we should fix but I’m just leaving a note here with my observations for other people that come across this.

1reaction
XamlTestcommented, Jun 7, 2023

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Android 13.0-API33 with provided Project: MauiApp1.zip

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.IllegalStateException: Can not perform this action ...
1 Answer 1 ... It is known as state loss. You happen to commit a FragmentTransaction from AsyncTask. That is prohibited by the...
Read more >
Xamarin.Forms: Java.Lang.IllegalStateException
In normal Android applications this usually doesn't happen too much as launching a new activity or swapping a fragment takes a bit of...
Read more >
crash after returning to swap (#409) · Issues · F-Droid / Client
I'm getting this crash quite a bit while playing around with swap. ... getActivityState(NfcActivityManager.java:173) E at android.nfc.
Read more >
How to Fix The IllegalStateException in Java
An IllegalStateException is a runtime exception in Java that is thrown to indicate that a method has been invoked at the wrong time....
Read more >
Clojure Programming: Practical Lisp for the Java World
(def n (atom 1 :validator pos?)) ;= #'user/n (swap! n + 500) ;= 501 (swap! n - 1000) ;= #<IllegalStateException java.lang.IllegalStateException: Invalid ...
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