Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

App resuming results in crash with FormsAppCompatActivity

I have a Xamarin Forms app, currently being built for Android. I have a MainActivity that used to extend FormsApplicationActivity, but because I want to use a custom theme I had to change it to extend FormsAppCompatActivity (see my other question: http://stackoverflow.com/questions/35754590/xamarin-forms-custom-theme-not-working).

Ever since changing from FormsApplicationActivity to FormsAppCompatActivity the app crashes whenever I switch out of the app and then back into the app. It throws an error in the App.xaml.cs class in the OnResume method, where I try to set the MainPage to a new navigation page:

protected override void OnResume()
{
    bool isRegistered = _authenticationService.IsRegistered();

    MainPage = isRegistered 
        ? new NavigationPage(new LoginPage()) 
        : new NavigationPage(new RegisterPage()); // this results in the crash
}

The error I'm getting is:

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

This is the stacktrace:

03-09 13:43:52.098 I/MonoDroid( 1243): Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-09 13:43:52.098 I/MonoDroid( 1243):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Android.Runtime.JNIEnv.CallIntMethod (IntPtr jobject, IntPtr jmethod) [0x00063] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:386 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Android.Support.V4.App.FragmentTransactionInvoker.Commit () [0x00033] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync (Xamarin.Forms.Page view, Boolean animated, Boolean removed, Boolean popToRoot) [0x000e1] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushAsync (Xamarin.Forms.Page view, Boolean animated) [0x00000] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PushViewAsync (Xamarin.Forms.Page page, Boolean animated) [0x00000] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.<OnElementChanged>b__13_0 (Xamarin.Forms.Page p) [0x00000] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.EnumerableExtensions.ForEach[T] (IEnumerable`1 enumeration, System.Action`1 action) [0x00010] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1 e) [0x001af] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (Xamarin.Forms.Platform.Android.TElement element) [0x000fc] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer (Xamarin.Forms.VisualElement view) [0x00000] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild (Xamarin.Forms.Page page, Boolean layout) [0x00015] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x00090] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0001a] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.AppOnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x0001e] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00012] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Application.set_MainPage (Xamarin.Forms.Page value) [0x0008b] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Smartbit.App.OnResume () [0x00020] in C:\Users\leonc\Documents\Visual Studio 2015\Projects\Smartbit\Smartbit\Smartbit\App.xaml.cs:52 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Application.SendResume () [0x00006] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnStateChanged () [0x00039] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnRestart () [0x00019] in <filename unknown>:0 
03-09 13:43:52.098 I/MonoDroid( 1243):   at Android.App.Activity.n_OnRestart (IntPtr jnienv, IntPtr native__this) [0x00009] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Activity.cs:4539 
03-09 13:43:52.098 I/MonoDroid( 1243):   at (wrapper dynamic-method) System.Object:cba7a870-1435-4f70-9059-e10915aba0c0 (intptr,intptr)
03-09 13:43:52.098 I/MonoDroid( 1243):   --- End of managed exception stack trace ---
03-09 13:43:52.098 I/MonoDroid( 1243): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1448)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1466)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
03-09 13:43:52.098 I/MonoDroid( 1243):  at md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.n_onRestart(Native Method)
03-09 13:43:52.098 I/MonoDroid( 1243):  at md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.onRestart(FormsAppCompatActivity.java:86)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1181)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.Activity.performRestart(Activity.java:5291)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.Activity.performResume(Activity.java:5302)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.os.Handler.dispatchMessage(Handler.java:102)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.os.Looper.loop(Looper.java:136)
03-09 13:43:52.098 I/MonoDroid( 1243):  at android.app.ActivityThread.main(ActivityThread.java:5001)
03-09 13:43:52.098 I/MonoDroid( 1243):  at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:43:52.098 I/MonoDroid( 1243):  at java.lang.reflect.Method.invoke(Method.java:515)
03-09 13:43:52.098 I/MonoDroid( 1243):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-09 13:43:52.098 I/MonoDroid( 1243):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-09 13:43:52.098 I/MonoDroid( 1243):  at dalvik.system.NativeStart.main(Native Method)

Best Answer

Answers

  • LeonCullensLeonCullens NLMember

    Anyone?

  • LeonCullensLeonCullens NLMember

    @NMackay said:
    @LeonCullens
    That might be related to this known issue I raised a while back with AppCompat

    https://bugzilla.xamarin.com/show_bug.cgi?id=36907

    Jason has posted a suggested workaround, it may not be related to your issue but as your issue is in OnResume it's worth checking out.

    Huh. That's the weirdest fix. Seems to be a workaround for my issue! I added a call to base.OnResume() in my OnResume method and added a Task.Delay(10).Wait() in both my OnResume method and my MainActivity.OnCreate. So far it seems to be working! Thank you :smile:

  • CrosevenCroseven USMember ✭✭

    I am facing the same problem, however Task.Delay is not working for me, I have the latest Xamarin for visual studio (as of todays date) and my problem is at await Navigation.PushAsync(page); right after I scanned a QR Code using the latest XZing.Mobile

  • Same problem.
    Sometimes crash on await NavigationPushAsync(page); or await Navigation.PopAsync();
    Using Xamarin Forms 2.3.2.127

  • AviiNLAviiNL USMember

    @Croseven said:
    I am facing the same problem, however Task.Delay is not working for me, I have the latest Xamarin for visual studio (as of todays date) and my problem is at await Navigation.PushAsync(page); right after I scanned a QR Code using the latest XZing.Mobile

    Did you manage to fix this? I have the exact same issue

  • CrosevenCroseven USMember ✭✭

    @AviiNL said:

    @Croseven said:
    I am facing the same problem, however Task.Delay is not working for me, I have the latest Xamarin for visual studio (as of todays date) and my problem is at await Navigation.PushAsync(page); right after I scanned a QR Code using the latest XZing.Mobile

    Did you manage to fix this? I have the exact same issue

    Yes I installed 'https://www.nuget.org/packages/ZXing.Net.Mobile.Forms/' instead of just ZXing.Net.Mobile and then just implemented that, seemed to have worked. So my code changed slightly to use the Form component

  • AviiNLAviiNL USMember

    @Croseven said:

    @AviiNL said:

    @Croseven said:
    I am facing the same problem, however Task.Delay is not working for me, I have the latest Xamarin for visual studio (as of todays date) and my problem is at await Navigation.PushAsync(page); right after I scanned a QR Code using the latest XZing.Mobile

    Did you manage to fix this? I have the exact same issue

    Yes I installed 'https://www.nuget.org/packages/ZXing.Net.Mobile.Forms/' instead of just ZXing.Net.Mobile and then just implemented that, seemed to have worked. So my code changed slightly to use the Form component

    Ah thanks, I already had the Zing.Net.Mobile.Forms loaded in but wasn't using the ScannerPage, Changed it to use the scanner page and it works :)

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    The issue most likely is just caused that you are attempting to transition too fast before the page is actually re-inflated. This usually can be caused by open a new activity such as the scanner page or login page. I would play around with the

    await Task.Delay(300);
    

    with different timings there and put a spinner perhaps.

  • BrightLeeBrightLee KRMember ✭✭✭

    If I tried set App.MainPage whlie app's on background. This runtime error occur.
    You can test it by doing like this. Run app from IDE, when smartphone is locked. (screen off)

    It will occur when changing App.MainPage.

    @JamesMontemangno

  • 15mgm1515mgm15 USMember ✭✭✭✭

    I have fixed this the MVVM way, bindings always, not blocking the UI with loading indicator and taking into account always the long transitions between pages a good solution will be:

    1.- Load the most simple UI.
    2.- If there is a web server call, let the user know with bindable objects like spinners or labels.
    3.- Let the user navigate until the server side calls finish on background thread.

    Something like Slack does:

  • BrightLeeBrightLee KRMember ✭✭✭

    @15mgm15
    I'm sure it's happened only on Android with AppCompat.

  • scinfuscinfu ITMember

    Any official solution ?

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    There is a fix for this and it's already merged.

  • SasanSasan USMember

    hi guys,

    is there any update on this thread? i have a similar issue and task delay sometimes work and sometimes don't

  • AhmedAlejoAhmedAlejo USMember ✭✭

    @AdrianKnight said:
    There is a fix for this and it's already merged.

    Hi there, how was the fix implemented?, i know it has to do with PlatformSpecifics

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    @AhmedAlejo It was a platform specific option initially but later got merged in as-is. https://github.com/xamarin/Xamarin.Forms/pull/527

  • AhmedAlejoAhmedAlejo USMember ✭✭

    @AdrianKnight said:
    @AhmedAlejo It was a platform specific option initially but later got merged in as-is. https://github.com/xamarin/Xamarin.Forms/pull/527

    CommitAllowingStateLoss did the trick.

    Is this released?

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    In 2.3.4-pre1.

  • mario.moramario.mora USMember ✭✭

    @AdrianKnight said:
    In 2.3.4-pre1.

    Just tested with the 2.3.4.184-pre1 but it does nothing.
    :neutral:

  • batmacibatmaci DEMember ✭✭✭✭✭

    anybody tested it with pre2?

  • ReinierKops.6845ReinierKops.6845 USUniversity

    Any comments??

  • ChrisMcBride.2625ChrisMcBride.2625 USUniversity ✭✭

    We waited for 2.3.4, hoping to fix this. As this is our #1 crash at the moment. Re released our app with 2.3.4 last week, and it didn't seem to have any effect.

  • TamasMatraiTamasMatrai HUUniversity ✭✭

    I am on 2.5.0 and still have this problem :open_mouth:

    UserDialogsImpl+<>c__DisplayClass27_0`2[TFragment,TConfig].b__0 ()
    Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState

    Can not perform this action after onSaveInstanceState

    1
    ExceptionDispatchInfo.Throw ()
    2
    JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)
    3
    JNIEnv.CallNonvirtualVoidMethod (System.IntPtr jobject, System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms)
    4
    DialogFragment.Show (Android.Support.V4.App.FragmentManager manager, System.String tag)
    5
    UserDialogsImpl+<>c__DisplayClass27_0`2[TFragment,TConfig].b__0 ()
    6
    Thread+RunnableImplementor.Run ()

  • requenaxiirequenaxii USMember ✭✭

    Same for me, I'm still getting this error; actually I receive each day more than 200 crashes related... how can I fix that? I'm really worried about this one, the crash report in Android Store is now over 20%!!

Sign In or Register to comment.