maui: Android Crash ImageSource Glyph
Description
Android app crashes (always) on app resume/re-activate when FontImageSource Glyph is used.
<Button Background="{StaticResource Primary}">
<Button.ImageSource>
<FontImageSource Glyph="+" FontFamily="fa-regular" Size="25" />
</Button.ImageSource>
</Button>
[AndroidRuntime] java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
Steps to Reproduce
- Create new MAUI App
- Add button (add fonts)
<Button Background="{StaticResource Primary}">
<Button.ImageSource>
<FontImageSource Glyph="+" FontFamily="fa-regular" Size="25" />
</Button.ImageSource>
</Button>
- Click Android back button - to minimize the app
- Restart the app
- Observe crash
Link to public reproduction project repository
https://github.com/get-flat/maui_issue_glyph
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 10
Did you find any workaround?
No response
Relevant log output
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: com.companyname.mauiapp4, PID: 11098
[AndroidRuntime] java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
[AndroidRuntime] at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:353)
[AndroidRuntime] at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:153)
[AndroidRuntime] at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:133)
[AndroidRuntime] at com.bumptech.glide.Glide.with(Glide.java:818)
[AndroidRuntime] at com.microsoft.maui.glide.MauiCustomTarget.lambda$clear$2$com-microsoft-maui-glide-MauiCustomTarget(MauiCustomTarget.java:61)
[AndroidRuntime] at com.microsoft.maui.glide.MauiCustomTarget$$ExternalSyntheticLambda2.run(Unknown Source:2)
[AndroidRuntime] at android.os.Handler.handleCallback(Handler.java:883)
[AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:100)
[AndroidRuntime] at android.os.Looper.loop(Looper.java:237)
[AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:8167)
[AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
[AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 3
- Comments: 15
Why the issue has been closed without answering my last question? Can you please answer? Thanks
@Zhanglirong-Winnie, sorry, what is the consequence of your analysis? The issue is resolved for newer Android version? We are experiencing the issue also on Android 13, as you can see in the following image.
Verified this issue with Visual Studio Enterprise 17.7.0 Preview 2.0. Not repro on android emulator 33 platform with sample project. I tried with Android emulator 23, this issue also repro. So please try it on the latest android emulator. maui.zip Android emulator 33:
Android emulator 23:

Hello, we have the same issue in our Shell app, on different devices and different Android versions (no recognizable patterns). We are not able to reproduce the issue but we find it often in our logs on Google Play Console. Please, take a look at this, it’s causing lots of crashes.
Hello @henda79 ,
i just can agree. Maybe, this is a better workaround for you. The problem is that when you are on your last screen in the view stack and press back, the main activity will be closed. After reopen the app it will re init the activity and the bug appears. For me this is anyway a bit strange because i do’t want that the activity will be closed and reinit again just because the user want to minimize the app. In this case add the following code to your MainActivity.cs.
public override void OnBackPressed() { var stackCount = Shell.Current.Navigation.NavigationStack.Count; if (stackCount == 1) { this.MoveTaskToBack(true); } else { base.OnBackPressed(); } }This will minimize your app instead of closing the activity. I use this also for my xamarin apps to avoid to always reinit the activity after a simple back click of the user.
I found a somewhat perverse solution to this problem, but it works. We will use a WeakReferenceMessenger from Community Toolkit.Mvvm.Messaging
First of all, I created a message to inform about the application lifecycle. I use it in several places of the application and it’s very useful.
using CommunityToolkit.Mvvm.Messaging.Messages;
public class AppEventMessage : ValueChangedMessage<AppEventMessageType> { public AppEventMessage(AppEventMessageType type) : base(type) { } }
public enum AppEventMessageType { Created, Activated, Deactivated, Stopped, Resumed, Destroying }
Next I have added to App.xaml.cs following code
protected override Window CreateWindow(IActivationState? activationState) { var window = base.CreateWindow(activationState);
Next I have created class AppButton that overrides Button and I use it everywhere instead of the standard button with font icon
using Maui.BindableProperty.Generator.Core; public partial class AppButton : Button { [AutoBindable] string? _iconGlyph;
if (ImageSource == null) { ImageSource = new FontImageSource(); ImageSource.SetBinding(FontImageSource.GlyphProperty, new Binding(nameof(IconGlyph), mode: BindingMode.OneWay, source: this)); ImageSource.SetBinding(FontImageSource.ColorProperty, new Binding(nameof(TextColor), mode: BindingMode.OneWay, source: this)); ImageSource.SetBinding(FontImageSource.SizeProperty, new Binding(nameof(FontSize), mode: BindingMode.OneWay, source: this)); } } }
The main idea is to set ImageSource to null when Application stopped and set it again when Application recreated. If anyone have an idea to make this solution more beautiful - please share it.