Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Crash ImageSource Glyph #12513

Open
get-flat opened this issue Jan 9, 2023 · 15 comments
Open

Android Crash ImageSource Glyph #12513

get-flat opened this issue Jan 9, 2023 · 15 comments
Labels
area-controls-image Image control delighter high It doesn't work at all, crashes or has a big impact. p/1 Work that is critical for the release, but we could probably ship without platform/android 🤖 s/triaged Issue has been reviewed t/bug Something isn't working

Comments

@get-flat
Copy link

get-flat commented Jan 9, 2023

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 get-flat added the t/bug Something isn't working label Jan 9, 2023
@jsuarezruiz jsuarezruiz added platform/android 🤖 area-image Image loading, sources, caching labels Jan 9, 2023
@jsuarezruiz jsuarezruiz added this to the Backlog milestone Jan 9, 2023
@ghost
Copy link

ghost commented Jan 9, 2023

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.

@FM1973
Copy link

FM1973 commented Jan 19, 2023

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

@kofanov
Copy link

kofanov commented Feb 18, 2023

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
{
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.

@stoff99
Copy link

stoff99 commented Apr 5, 2023

Vote up! Have the same issue.

@henda79
Copy link

henda79 commented Apr 7, 2023

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.

@stoff99
Copy link

stoff99 commented Apr 8, 2023

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.

@henda79
Copy link

henda79 commented Apr 9, 2023

@stoff99 , thank you.

@henda79
Copy link

henda79 commented May 11, 2023

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.

@stoff99
Copy link

stoff99 commented May 12, 2023

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.

@mgungorchamp
Copy link

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

@michele-guion
Copy link

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.

@Zhanglirong-Winnie Zhanglirong-Winnie added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Jul 5, 2023
@ghost
Copy link

ghost commented Jul 5, 2023

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.

@Zhanglirong-Winnie
Copy link
Collaborator

Zhanglirong-Winnie commented Jul 5, 2023

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

@michele-guion
Copy link

@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

@ghost ghost closed this as completed Jul 14, 2023
@michele-guion
Copy link

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

@ghost ghost locked as resolved and limited conversation to collaborators Aug 13, 2023
@samhouts samhouts reopened this Sep 13, 2023
@samhouts samhouts added p/1 Work that is critical for the release, but we could probably ship without and removed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Sep 13, 2023
@samhouts samhouts modified the milestones: Backlog, .NET 8 SR2 Oct 3, 2023
@PureWeen PureWeen added the high It doesn't work at all, crashes or has a big impact. label Jan 10, 2024
@Redth Redth modified the milestones: .NET 8 SR2, .NET 8 + Servicing Jan 10, 2024
@Eilon Eilon added area-controls-image Image control and removed area-image Image loading, sources, caching labels May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-image Image control delighter high It doesn't work at all, crashes or has a big impact. p/1 Work that is critical for the release, but we could probably ship without platform/android 🤖 s/triaged Issue has been reviewed t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests