Open get-flat opened 1 year ago
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.
Happens now and then in our app too. But not every time.
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 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.
Vote up! Have the same issue.
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.
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 , thank you.
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.
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.
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
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.
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.
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:
@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.
Why the issue has been closed without answering my last question? Can you please answer? Thanks
Description
Android app crashes (always) on app resume/re-activate when FontImageSource Glyph is used.
[AndroidRuntime] java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
Steps to Reproduce
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