maui icon indicating copy to clipboard operation
maui copied to clipboard

Android Crash ImageSource Glyph

Open get-flat opened this issue 3 years ago • 15 comments

Description

Android app crashes (always) on app resume/re-activate when FontImageSource Glyph is used.

<Button Background="{StaticResource Primary}">
    <Button.ImageSource>
        <FontImageSource Glyph="&#x2b;" FontFamily="fa-regular" Size="25" />
    </Button.ImageSource>
</Button>

[AndroidRuntime] java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity

Steps to Reproduce

  1. Create new MAUI App
  2. Add button (add fonts)
<Button Background="{StaticResource Primary}">
    <Button.ImageSource>
        <FontImageSource Glyph="&#x2b;" FontFamily="fa-regular" Size="25" />
    </Button.ImageSource>
</Button>
  1. Click Android back button - to minimize the app
  2. Restart the app
  3. 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)

get-flat avatar Jan 09 '23 12:01 get-flat

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Jan 09 '23 15:01 ghost

Happens now and then in our app too. But not every time.

FM1973 avatar Jan 19 '23 12:01 FM1973

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);

    window.Created += (s,e) => {
        WeakReferenceMessenger.Default.Send(new AppEventMessage(AppEventMessageType.Created));
    };

    window.Activated += (s, e) => {
        WeakReferenceMessenger.Default.Send(new AppEventMessage(AppEventMessageType.Activated));
    };

    window.Deactivated += (s, e) => {
        WeakReferenceMessenger.Default.Send(new AppEventMessage(AppEventMessageType.Deactivated));
    };

    window.Stopped += (s, e) => {
        WeakReferenceMessenger.Default.Send(new AppEventMessage(AppEventMessageType.Stopped));
    };

    window.Resumed += (s, e) => {
        WeakReferenceMessenger.Default.Send(new AppEventMessage(AppEventMessageType.Resumed));
    };

    window.Destroying += (s, e) => {
        WeakReferenceMessenger.Default.Send(new AppEventMessage(AppEventMessageType.Destroying));
    };

    return window;
}

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;

public AppButton()
{
    #if ANDROID
        WeakReferenceMessenger.Default.UnregisterAll(this);
        WeakReferenceMessenger.Default.Register<AppEventMessage>(this, (r, m) => {
            if (m.Value == AppEventMessageType.Stopped)
                ImageSource = null;
            else if (m.Value == AppEventMessageType.Created)
                SetImageSource();
        });
    #endif

    SetImageSource();
}

void SetImageSource()
{

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.

kofanov avatar Feb 18 '23 17:02 kofanov

Vote up! Have the same issue.

stoff99 avatar Apr 05 '23 07:04 stoff99

It seems that we are better off using Xamarin for anything but the simplest of Apps as MAUI has many bugs and limitations which just seem to get pushed aside. I mean, this issue is problematic, it's not like it's a fringe case, yet we have to use workarounds like this one from @kofanov.

It's like the early days of Xamarin all over again.

henda79 avatar Apr 07 '23 22:04 henda79

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.

stoff99 avatar Apr 08 '23 08:04 stoff99

@stoff99 , thank you.

henda79 avatar Apr 09 '23 11:04 henda79

This works when the app is closed using the back button, although there is a warning about overriding an obsolete method, so who knows how long this will work for.

henda79 avatar May 11 '23 18:05 henda79

Hello @henda79 , i know but i didn't investigated into this right now. With Android 13 they introduced the predictive back gestures which makes this method obsolete. So the approach is the same, just the method is changing. After a quick search i found this: https://vladislavantonyuk.github.io/articles/Migrate-the-deprecated-OnBackPressed-function-in-.NET-MAUI-Android-application/

If i find some time i will implement it in this way.

stoff99 avatar May 12 '23 08:05 stoff99

Any solution to this In my case, it prepends the "File: " text to my file name... IDK why

Below I tried to print as a label the same text on the message below the image.Source attribute ... I don't have any control, over why this is happening when I minimize the app and move to another app then come back to my app... this happens

image

mgungorchamp avatar Jun 19 '23 23:06 mgungorchamp

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.

michele-guion avatar Jul 04 '23 14:07 michele-guion

Hi @get-flat. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Jul 05 '23 06:07 ghost

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: 12513-33 Android emulator 23: 12513

Zhanglirong-Winnie avatar Jul 05 '23 06:07 Zhanglirong-Winnie

@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. image

michele-guion avatar Jul 06 '23 17:07 michele-guion

Why the issue has been closed without answering my last question? Can you please answer? Thanks

michele-guion avatar Jul 14 '23 06:07 michele-guion