Skip to content

Running an Android app with R8 shrinking results in Java.Lang.IllegalStateException: FragmentManager has not been attached to a host. #26074

Open

Description

Description

Running the sample app in AdHoc config crashes with Java.Lang.IllegalStateException: FragmentManager has not been attached to a host.

Although this is Android-specific, the stack trace is in MAUI code, so this is where the issue is entered.

There is an attempt to preserve code using ProGuard configuration. It's possible there is some code being stripped out that is needed, but it's not clear what that is.

For some reason, Release mode doesn't seem to use the shrinking(?) Could be something wrong I have set in my project.

Steps to Reproduce

  1. Run the reproduction app in AdHoc configuration.
  2. The app crashes with the error.

Link to public reproduction project repository

https://github.com/brunck/reproductions/tree/master/AndroidFragmentManagerError

Version with bug

9.0.0 GA

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

35

Did you find any workaround?

Don't use code shrinking/ProGuard configuration.

Relevant log output

Stack Trace
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.RuntimeException: Unable to start activity ComponentInfo{com.companyname.androidfragmentmanagererror/crc6401489ec77a456728.MainActivity}: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid]  ---> Java.Lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid]    at Java.Interop.JniEnvironment.InstanceMethods.CallIntMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args)
[MonoDroid]    at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeAbstractInt32Method(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
[MonoDroid]    at AndroidX.Fragment.App.BackStackRecord.Commit()
[MonoDroid]    at Microsoft.Maui.Handlers.FlyoutViewHandler.<UpdateDetailsFragmentView>b__11_1(FragmentManager fm)
[MonoDroid]    at Microsoft.Maui.Platform.FragmentManagerExtensions.RunOrWaitForResume(FragmentManager obj, Context context, Action`1 onResume)
[MonoDroid]    at Microsoft.Maui.Handlers.FlyoutViewHandler.UpdateDetailsFragmentView()
[MonoDroid]    at Microsoft.Maui.Handlers.FlyoutViewHandler.LayoutAsFlyout()
[MonoDroid]    at Microsoft.Maui.Handlers.FlyoutViewHandler.LayoutViews()
[MonoDroid]    at Microsoft.Maui.Handlers.FlyoutViewHandler.UpdateFlyout()
[MonoDroid]    at Microsoft.Maui.Handlers.FlyoutViewHandler.MapFlyout(IFlyoutViewHandler handler, IFlyoutView flyoutView)
[MonoDroid]    at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IFlyoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IFlyoutViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
[MonoDroid]    at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
[MonoDroid]    at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
[MonoDroid]    at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
[MonoDroid]    at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IFlyoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IView view)
[MonoDroid]    at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IFlyoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IElement view)
[MonoDroid]    at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
[MonoDroid]    at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
[MonoDroid]    at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
[MonoDroid]    at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
[MonoDroid]    at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
[MonoDroid]    at Microsoft.Maui.Platform.ContainerView.SetView(IElement view, Boolean forceRefresh)
[MonoDroid]    at Microsoft.Maui.Platform.ContainerView.set_CurrentView(IElement value)
[MonoDroid]    at Microsoft.Maui.Platform.ElementExtensions.ToContainerView(IElement view, IMauiContext context)
[MonoDroid]    at Microsoft.Maui.Platform.NavigationRootManager.Connect(IView view, IMauiContext mauiContext)
[MonoDroid]    at Microsoft.Maui.Handlers.WindowHandler.CreateRootViewFromContent(IWindowHandler handler, IWindow window)
[MonoDroid]    at Microsoft.Maui.Handlers.WindowHandler.MapContent(IWindowHandler handler, IWindow window)
[MonoDroid]    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)
[MonoDroid]    at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
[MonoDroid]    at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
[MonoDroid]    at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
[MonoDroid]    at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
[MonoDroid]    at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
[MonoDroid]    at Microsoft.Maui.Platform.ElementExtensions.SetHandler(Context nativeElement, IElement element, IMauiContext context)
[MonoDroid]    at Microsoft.Maui.Platform.ElementExtensions.SetWindowHandler(Activity platformWindow, IWindow window, IMauiContext context)
[MonoDroid]    at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState)
[MonoDroid]    at Microsoft.Maui.MauiAppCompatActivity.OnCreate(Bundle savedInstanceState)
[MonoDroid]    at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState)
[MonoDroid]    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0)
[MonoDroid]   --- End of managed Java.Lang.IllegalStateException stack trace ---
[MonoDroid] java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid] 	at androidx.fragment.app.FragmentManager.enqueueAction(Unknown Source:22)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commitInternal(Unknown Source:67)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commit(Unknown Source:1)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.n_onCreate(Native Method)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.onCreate(Unknown Source:0)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8342)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8321)
[MonoDroid] 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
[MonoDroid] 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
[MonoDroid] 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
[MonoDroid] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:106)
[MonoDroid] 	at android.os.Looper.loopOnce(Looper.java:201)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:288)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:7918)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Native Method)
[MonoDroid] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
[MonoDroid] 
[MonoDroid]   --- End of managed Java.Lang.IllegalStateException stack trace ---
[MonoDroid] java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid] 	at androidx.fragment.app.FragmentManager.enqueueAction(Unknown Source:22)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commitInternal(Unknown Source:67)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commit(Unknown Source:1)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.n_onCreate(Native Method)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.onCreate(Unknown Source:0)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8342)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8321)
[MonoDroid] 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
[MonoDroid] 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
[MonoDroid] 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
[MonoDroid] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:106)
[MonoDroid] 	at android.os.Looper.loopOnce(Looper.java:201)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:288)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:7918)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Native Method)
[MonoDroid] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
[MonoDroid] 
[MonoDroid]    --- End of inner exception stack trace ---
[MonoDroid] 
[MonoDroid]   --- End of managed Java.Lang.RuntimeException stack trace ---
[MonoDroid] java.lang.RuntimeException: Unable to start activity ComponentInfo{com.companyname.androidfragmentmanagererror/crc6401489ec77a456728.MainActivity}: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3644)
[MonoDroid] 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
[MonoDroid] 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
[MonoDroid] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:106)
[MonoDroid] 	at android.os.Looper.loopOnce(Looper.java:201)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:288)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:7918)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Native Method)
[MonoDroid] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
[MonoDroid] Caused by: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid] 	at androidx.fragment.app.FragmentManager.enqueueAction(Unknown Source:22)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commitInternal(Unknown Source:67)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commit(Unknown Source:1)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.n_onCreate(Native Method)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.onCreate(Unknown Source:0)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8342)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8321)
[MonoDroid] 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
[MonoDroid] 	... 12 more
[MonoDroid] 
[MonoDroid]   --- End of managed Java.Lang.RuntimeException stack trace ---
[MonoDroid] java.lang.RuntimeException: Unable to start activity ComponentInfo{com.companyname.androidfragmentmanagererror/crc6401489ec77a456728.MainActivity}: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3644)
[MonoDroid] 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
[MonoDroid] 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
[MonoDroid] 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
[MonoDroid] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:106)
[MonoDroid] 	at android.os.Looper.loopOnce(Looper.java:201)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:288)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:7918)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Native Method)
[MonoDroid] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
[MonoDroid] Caused by: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
[MonoDroid] 	at androidx.fragment.app.FragmentManager.enqueueAction(Unknown Source:22)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commitInternal(Unknown Source:67)
[MonoDroid] 	at androidx.fragment.app.BackStackRecord.commit(Unknown Source:1)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.n_onCreate(Native Method)
[MonoDroid] 	at crc6488302ad6e9e4df1a.MauiAppCompatActivity.onCreate(Unknown Source:0)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8342)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:8321)
[MonoDroid] 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
[MonoDroid] 	... 12 more
[MonoDroid] 
[Process] Sending signal. PID: 7676 SIG: 9

Metadata

Assignees

No one assigned

    Labels

    t/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions