Bug 51546 - Hardware/Software back button from MainPage of type MasterDetail causes crash 'java.lang.IllegalStateException: Activity has been destroyed'
Summary: Hardware/Software back button from MainPage of type MasterDetail causes crash...
Status: RESOLVED NORESPONSE
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Samantha Houts [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-01-15 13:21 UTC by Mohamed
Modified: 2017-06-20 00:01 UTC (History)
6 users (show)

Tags: android
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Mohamed 2017-01-15 13:21:53 UTC
Overview:
I'm seeing an exception ('java.lang.IllegalStateException: Activity has been destroyed') that crashes my app when the user uses the hardware/software back button from the root MainPage of my app when it is of type MasterDetail. If I use a regular Page instead of MasterDetail as my MainPage, the crash does not occur.


Steps to Reproduce:
1) Setup MainPage to use a MasterDetail. 
2) After app loads the MasterDetail, use the hardware or software back button which will close application since it is the root Page.
3) Observe exception

Build Date & Hardware:
Occurs on Xamarin.Forms 2.3.4.184-pre1 running on Honor 5C

Stack trace: 

Unhandled Exception:

Java.Lang.IllegalStateException: Activity has been destroyed

01-14 00:09:03.791 I/MonoDroid( 8009): UNHANDLED EXCEPTION:
01-14 00:09:03.807 I/MonoDroid( 8009): Java.Lang.IllegalStateException: Activity has been destroyed
01-14 00:09:03.807 I/MonoDroid( 8009):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
01-14 00:09:03.807 I/MonoDroid( 8009):   at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method) [0x00084] in /Users/builder/data/lanes/3511/501e63ce/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11464 
01-14 00:09:03.807 I/MonoDroid( 8009):   at Android.Runtime.JNIEnv.CallIntMethod (System.IntPtr jobject, System.IntPtr jmethod) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/JNIEnv.g.cs:186 
01-14 00:09:03.807 I/MonoDroid( 8009):   at Android.Support.V4.App.FragmentTransactionInvoker.CommitAllowingStateLoss () [0x00033] in <27c17fe440cf491ba8255bcefade6e02>:0 
01-14 00:09:03.807 I/MonoDroid( 8009):   at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailContainer.Dispose (System.Boolean disposing) [0x00042] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\MasterDetailContainer.cs:130 
01-14 00:09:03.807 I/MonoDroid( 8009):   at Java.Lang.Object.Dispose () [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:203 
01-14 00:09:03.808 I/MonoDroid( 8009):   at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer.Dispose (System.Boolean disposing) [0x00046] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\MasterDetailPageRenderer.cs:192 
01-14 00:09:03.808 I/MonoDroid( 8009):   at Java.Lang.Object.Dispose () [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:203 
01-14 00:09:03.808 I/MonoDroid( 8009):   at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x0003f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:226 
01-14 00:09:03.808 I/MonoDroid( 8009):   at Xamarin.Forms.Platform.Android.AppCompat.Platform.Dispose () [0x00010] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:52 
01-14 00:09:03.808 I/MonoDroid( 8009):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnDestroy () [0x0002f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:195 
01-14 00:09:03.808 I/MonoDroid( 8009):   at MasjidIDA.Droid.MainActivity.OnDestroy () [0x00012] in C:\Users\moham\Documents\Git\MasjidIDA\MasjidIDA\MasjidIDA.Droid\MainActivity.cs:80 
01-14 00:09:03.808 I/MonoDroid( 8009):   at Android.App.Activity.n_OnDestroy (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.App.Activity.cs:2981 
01-14 00:09:03.808 I/MonoDroid( 8009):   at (wrapper dynamic-method) System.Object:169ee778-334d-4161-b428-9dd4fec3055f (intptr,intptr)
01-14 00:09:03.808 I/MonoDroid( 8009):   --- End of managed Java.Lang.IllegalStateException stack trace ---
01-14 00:09:03.808 I/MonoDroid( 8009): java.lang.IllegalStateException: Activity has been destroyed
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at md585057ebc46e14ad63423b345b13af9ee.MainActivity.n_onDestroy(Native Method)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at md585057ebc46e14ad63423b345b13af9ee.MainActivity.onDestroy(MainActivity.java:56)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.Activity.performDestroy(Activity.java:6567)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1145)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3897)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3929)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.ActivityThread.access$1500(ActivityThread.java:168)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.os.Handler.dispatchMessage(Handler.java:102)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.os.Looper.loop(Looper.java:150)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at android.app.ActivityThread.main(ActivityThread.java:5665)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at java.lang.reflect.Method.invoke(Native Method)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
01-14 00:09:03.808 I/MonoDroid( 8009): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
Comment 1 Tiffany 2017-01-16 20:44:50 UTC
Thanks Mohammed for opening another bug. Confirming this issue still occurs on main page of MasterDetailPage with a hardware back button. 

Previously marked as 'fixed' https://bugzilla.xamarin.com/show_bug.cgi?id=46494

Does not occur on FormsApplicationActivity but rather FormsAppCompatActivity for me.
Comment 2 Mohamed 2017-01-16 22:39:27 UTC
I have done some testing with different versions of Xamarin Forms and using FormsAppCompatActivity and FormsApplicationActivity.

Xamarin Forms 2.3.3.180
 ** FormsApplicationActivity : No bug
 ** FormsAppCompatActivity : Bug occur

Xamarin Forms 2.3.4.184-pre1
 ** FormsApplicationActivity : No bug
 ** FormsAppCompatActivity : Bug occur
Comment 3 Tiffany 2017-02-01 17:46:31 UTC
Dear Xamarin developers,

Could you make sure you are using FormsAppCompatActivity and NOT the regular FormsApplicationActivity.

Any updates on this? I've received a custom 2.3.3 nuget package from one of your developers (on CI marked as 9.9.0.760-pre1) and the issue still occurs on the root page of MasterDetailPage.

My side menu has several pages, I was able to switch back to root page from another page using the hardware back button. But once on the root page, it causes a crash if I try to press the hardware back button (to force app to go to background normally) with the following:

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.IllegalStateException: Activity has been destroyed
[mono-rt]   --- End of managed Java.Lang.IllegalStateException stack trace ---
[mono-rt] java.lang.IllegalStateException: Activity has been destroyed
[mono-rt] 	at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
[mono-rt] 	at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
[mono-rt] 	at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)
[mono-rt] 	at md5ce736d40a58ed915fbed222741666874.BaseMainActivity.n_onDestroy(Native Method)
[mono-rt] 	at md5ce736d40a58ed915fbed222741666874.BaseMainActivity.onDestroy(BaseMainActivity.java:73)
[mono-rt] 	at android.app.Activity.performDestroy(Activity.java:6407)
[mono-rt] 	at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1142)
[mono-rt] 	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3818)
[mono-rt] 	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3849)
[mono-rt] 	at android.app.ActivityThread.-wrap5(ActivityThread.java)
[mono-rt] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
[mono-rt] 	at android.os.Handler.dispatchMessage(Handler.java:102)
[mono-rt] 	at android.os.Looper.loop(Looper.java:148)
[mono-rt] 	at android.app.ActivityThread.main(ActivityThread.java:5417)
[mono-rt] 	at java.lang.reflect.Method.invoke(Native Method)
[mono-rt] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
[mono-rt] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
[mono-rt]
Comment 4 Samantha Houts [MSFT] 2017-02-15 22:17:42 UTC
The fix for Bug 46494 was done in the AppCompat backend, as you can see here: https://github.com/xamarin/Xamarin.Forms/pull/505/files.

Can you please provide a reproduction project to show that the fix did not resolve your particular issue?

Warm regards,
Xamarin.Forms Team
Comment 5 Samantha Houts [MSFT] 2017-06-20 00:01:01 UTC
Because we have not received a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket with the requested information. Thanks!