This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 36907 - [Android] PopToRootAsync() Not Working From OnResume() When Using FormsAppCompatActivity
Summary: [Android] PopToRootAsync() Not Working From OnResume() When Using FormsAppCom...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android (show other bugs)
Version: 2.0.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-12-11 18:11 UTC by Jimmy Garrido
Modified: 2017-04-12 11:28 UTC (History)
30 users (show)

See Also:
Tags: ac
Is this bug a regression?: ---
Last known good build:


Attachments
repro solution (1.21 MB, application/zip)
2015-12-11 18:11 UTC, Jimmy Garrido
Details

Description Jimmy Garrido 2015-12-11 18:11:11 UTC
Created attachment 14253 [details]
repro solution

### Overview
On Android, PopToRootAsync() does not work from OnResume() when using FormsAppCompatActivity.


### Steps to Reproduce
1. Run the attached Android project
2. Select ListView item to go to a detail page
3. Press the Android home button to background the app
4. Resume the app from the recent app list


### Expected Results
The app will return to the first page with the ListView.


### Actual Results
The app will stay on the item detail page. Also you cannot return to the start page by pressing the back button in the toolbar. Looking at the app locals through the debugger, the navigation stack contains only the start page and the NavPage’s CurrentPage is also set to the start page. So it looks  the PopToRootAsync() operation occurred, but the app did not actually navigate to the page.


### Environment Info
=== Xamarin Studio ===

Version 5.10.1 (build 6)
Installation UUID: 94ce5106-6a72-4691-b34e-cd5857b1db66
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Version: 0.24.0.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.iOS ===

Version: 9.4.0.0 (Business Edition)
Hash: 7322991
Branch: master
Build date: 2015-12-08 16:20:29-0500

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/jimmygarrido/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 22.0.1

Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.4.0.109 (Business Edition)

=== Build Information ===

Release ID: 510010006
Git revision: 0b60eecdb531933734519c13257d16a780274aab
Build date: 2015-12-04 20:28:20-05
Xamarin addins: 9876fd7c9837977178411ec7375b4352c0a0d6af
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.1
Darwin Jimmys-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0
    Sat Sep 19 15:53:46 PDT 2015
    root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 2 Marco 2015-12-27 13:39:14 UTC
I have the same problem and I can add this:

Using FormsAppCompatActivity the navigation doesn't work anymore.

I can use PushAsync and PopAsync, but if I try to use InsertPageBefore and then PopAsync or if I use RemovePage, then this doesn't work.


If I switch to FormsApplicationActivity then all works well.
Comment 3 Jason Smith 2016-03-10 18:49:05 UTC
I just ran the reproduction case and it seems to work? What device did you test on?
Comment 4 Jason Smith 2016-03-10 18:52:32 UTC
Nevermind, seems to only happen in pre-5.0 phones
Comment 5 Jason Smith 2016-03-10 18:56:15 UTC
You can workaround the issue by doing:

protected async override void OnResume()
        {
            base.OnResume();

	        await Task.Delay (10);
            // Lets start from the beginning again
            var nav = ServiceLocator.Current.GetInstance<INavService>();
            nav.Home();
        }

The issue is that the fragment based navigation on AppCompat on Pre 5.0 devices doesn't seem to enjoy working inside of OnResume.
Comment 6 Jani Lirkki 2016-09-14 14:52:58 UTC
Still reproducible in latest Xamarin Forms version 2.3.2.127 on my Android 5.1.1 phone.
Comment 7 Bright Lee 2016-09-24 20:02:39 UTC
It's still problem even above Android 5.0.
Comment 8 Sascha Schwegelbauer 2016-10-08 23:41:15 UTC
Problem still present - adding a delay into random methods in order to workaround a bug known for a year? in a professional framework? really?
Comment 9 adrianknight89 2016-10-12 22:43:35 UTC
I could not reproduce this in XF 2.3.3.152-pre2 / Samsung S6 / API 23. Not sure if it's an issue on >=21.
Comment 10 Christopher Sawyer 2016-10-31 03:12:16 UTC
I am getting this problem as well on Android 4.4.4 and 5.01 tests and on a physical HTC device running 5.x Android.

The Java stack apparently is not happy with being asked to throw on views while it's busy doing something OnResume.
Comment 11 Zahi Kramer 2016-11-16 18:07:54 UTC
Same problem with me.
My workaround for this was to force new page on App.Current.MainPage e.i.:
App.Current.MainPage = new HomePage();
Thus, resetting the activities stack (i think...)
Comment 12 Andrew 2016-11-28 16:45:01 UTC
same error
Comment 14 CAO Yang 2017-01-26 12:10:08 UTC
I have the same problem.

"MainActivity" was derived from "FormsApplicationActivity". In order to use the AppCompat theme of Android, the MainActivity has turned into "FormsAppCompatActivity". Then the problem occured: Navigation.PopAsync() wouldn't do its work any more.
Help please to fix it.
Comment 15 Lars Bøggild 2017-02-06 11:23:37 UTC
I see the same problem as CAO Yang on the Navigation.PopAsyc()..
Comment 16 Diego 2017-02-09 18:22:23 UTC
I got the same issue when the app is working in background:

Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () 
at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method)
at Android.Runtime.JNIEnv.CallIntMethod (System.IntPtr jobject, System.IntPtr jmethod) 
at Android.Support.V4.App.FragmentTransactionInvoker.Commit () 
at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailContainer.AddChildView (Xamarin.Forms.VisualElement childView)
at Xamarin.Forms.Platform.Android.MasterDetailContainer.set_ChildView (Xamarin.Forms.VisualElement value)
at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer.UpdateDetail () [
at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) 
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element)
at Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild (Xamarin.Forms.Page page, System.Boolean layout) 
at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage (Xamarin.Forms.Page newRoot) 
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) 
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.AppOnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args)
at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) 
at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) 
at Xamarin.Forms.Application.set_MainPage (Xamarin.Forms.Page value)
Comment 19 Neil Pepper 2017-03-02 16:18:20 UTC
Am getting the same issue, after backgrounding the app in release mode only, and reopening it then navigating around the app, most commonly after launching a model screen , but not always it always blows up eventually. Its fine if i dont background and its fine in debug.

Have tried the OnResume trick above

protected async override void OnResume()
        {
            base.OnResume();

	        await Task.Delay (10);
            // Lets start from the beginning again
            var nav = ServiceLocator.Current.GetInstance<INavService>();
            nav.Home();
        }

Assume Home function just sets the main page, as described by Zahi Kramer 

i.e.
App.Current.MainPage = new HomePage();

But no luck, nothing seems to work.

Any one manage to find a solution.

thanks Neil

Am using latest forms 2.3.3 193, but have also tried 2.3.4 192-pre2 and the nightly build. 6.0.1 android.

Stack Trace.
caused.AggregateException(s)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw():0
Java.Interop.JniEnvironment.InstanceMethods.CallIntMethod(JniObjectReference instance, JniMethodInfo method):0
Android.Runtime.JNIEnv.CallIntMethod(IntPtr jobject, IntPtr jmethod):0
Android.Support.V4.App.FragmentTransactionInvoker.CommitAllowingStateLoss()<27c17fe440cf491ba8255bcefade6e02>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync(Page page, bool animated, bool removed, bool popToRoot)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushAsync(Page view, bool animated)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PushViewAsync(Page page, bool animated)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnElementChanged(ElementChangedEventArgs e)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.VisualElementRenderer.SetElement(TElement element)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.VisualElementRenderer.SetElement(VisualElement element)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.Platform.CreateRenderer(VisualElement element)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.Platform.ModalContainer.Platform(Context context, Page modal)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.Platform.PresentModal(Page modal, bool animated)<00979a7978ab4144a58919b163f63751>:0
at Xamarin.Forms.Platform.Android.AppCompat.Platform+d__30.MoveNext () [0x0004a] in <00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Application.NavigationImpl.d__3.MoveNext():0
--- End of inner exception stack trace ---
---> (Inner Exception #0) Java.Lang.RuntimeException: Activity has been destroyed
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw():0
Java.Interop.JniEnvironment.InstanceMethods.CallIntMethod(JniObjectReference instance, JniMethodInfo method):0
Android.Runtime.JNIEnv.CallIntMethod(IntPtr jobject, IntPtr jmethod):0
Android.Support.V4.App.FragmentTransactionInvoker.CommitAllowingStateLoss()<27c17fe440cf491ba8255bcefade6e02>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync(Page page, bool animated, bool removed, bool popToRoot)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushAsync(Page view, bool animated)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PushViewAsync(Page page, bool animated)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnElementChanged(ElementChangedEventArgs e)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.VisualElementRenderer.SetElement(TElement element)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.VisualElementRenderer.SetElement(VisualElement element)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.Platform.CreateRenderer(VisualElement element)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.Platform.ModalContainer.Platform(Context context, Page modal)<00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Platform.Android.AppCompat.Platform.PresentModal(Page modal, bool animated)<00979a7978ab4144a58919b163f63751>:0
at Xamarin.Forms.Platform.Android.AppCompat.Platform+d__30.MoveNext () [0x0004a] in <00979a7978ab4144a58919b163f63751>:0
Xamarin.Forms.Application.NavigationImpl.d__3.MoveNext():0


Xamarin Studio Enterprise
Version 6.2 (build 1821)
Installation UUID: 0d980f6f-57aa-44e5-8995-12c9e34764af
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/e4a3cf3) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000495

NuGet
Version: 3.5.0.0

Xamarin.Profiler
Version: 0.22.0.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Xamarin.Android
Version: 7.1.0.41 (Visual Studio Enterprise)
Android SDK: /Volumes/DataDrive/android/sdk
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 25.1.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Xamarin Android Player
Not Installed

Apple Developer Tools
Xcode 8.2.1 (11766.1)
Build 8C1002

Xamarin.Mac
Version: 3.0.0.393 (Visual Studio Enterprise)

Xamarin Inspector
Not Installed

Xamarin.iOS
Version: 10.4.0.123 (Visual Studio Enterprise)
Hash: 35d1ccd
Branch: cycle9
Build date: 2017-02-16 17:40:00-0500

Build Information
Release ID: 602001821
Git revision: d41b6e51f3fa46a1943f2e31a778d28a7c73d069
Build date: 2017-02-17 15:18:19-05
Xamarin addins: 1363a8d943bab7700c93a97474060b6734aa7f94
Build lane: monodevelop-lion-cycle9

Operating System
Mac OS X 10.12.3
Darwin Neils-MBP.home 16.4.0 Darwin Kernel Version 16.4.0
    Thu Dec 22 22:53:21 PST 2016
    root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

Enabled user installed addins
SpecFlow Support 1.9.1.0
Comment 20 CAO Yang 2017-03-02 17:20:36 UTC
Here's my solution published on SO. Anyway that works.

http://stackoverflow.com/questions/40397431/xamarin-forms-popasync-crash/41897674#41897674
Comment 21 Neil Pepper 2017-03-07 08:38:25 UTC
ok for me this was caused by HockeyApp (which was already removed in debug), removed hockeyapp for release too app behaved happily. Hope it helps someone else.
Comment 22 Sascha Schwegelbauer 2017-03-07 08:46:44 UTC
Did you completely remove HockeyApp (also the reference) or only your initialization code?
Comment 23 Neil Pepper 2017-03-07 08:50:26 UTC
just commented out the initialisation code.
Comment 24 E.Z. Hart 2017-03-27 22:02:36 UTC
Should be fixed in 2.3.4.
Comment 25 Moni 2017-04-12 11:28:23 UTC
 I am still facing the same issue. I have updated the xamarin forms to 2.3.4 but issue still exits.

Note You need to log in before you can comment on or make changes to this bug.