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

Avalonia Android System.InvalidOperationException in OnCreate of MainActivity when notification tapped #17943

Open
sessiontalk opened this issue Jan 10, 2025 · 3 comments
Labels

Comments

@sessiontalk
Copy link

Describe the bug

Hi, On Avalonia Android bleeding edge branch 11.3.999-cibuild0054104-alpha (also happens on 11.2.3 stable) the below exception
is thrown in onCreate of MainActivity under numerous circumstances,

System.InvalidOperationException: 'The control MobileMainView (Content = Grid) already has a visual parent ContentPresenter (Name = PART_ContentPresenter, Host = EmbeddableControlRoot) while trying to add it as a child of ContentPresenter (Name = PART_ContentPresenter, Host = EmbeddableControlRoot).'

when tapping the launcher icon to launch app again, when tapping notification to launch Activity both when the MainActivity is in backgroun or foreground. This happens if the root view has a single Grid or any control. We have tried various launch modes in the Android manifest singletop, single instance,singletask
but this issue will happen at some point.All flags have been tested when setting pending intents for the activity when launching from notification, the issue's ocurrence can be reduced but not eliminated which suggests there is a bug that needs to be fixed

To Reproduce

Launch MainActivity from notification

Expected behavior

Mainactivity launches without throwing exception

Avalonia version

11.2.3

OS

No response

Additional context

No response

@IsaMorphic
Copy link

Hey there! I was experiencing similar behavior at first with my own mobile app, SubverseIM. My solution was to adjust the LaunchMode for the MainActivity class in the Android subproject to LaunchMode.SingleInstance. This seemed to prevent a duplicate call to OnCreate whenever I launched the activity from a notification.

I also had to adjust the way I initialized the notification object in order to replicate the behavior of launching the app from the home screen upon tapping the notification. Here's the relevant code:

// Configure intent for special "launcher" behavior
Intent notifyIntent = new Intent(this, typeof(MainActivity));
notifyIntent.SetAction(Intent.ActionMain);
notifyIntent.AddCategory(Intent.CategoryLauncher); 
notifyIntent.AddFlags(ActivityFlags.NewTask);

// Create PendingIntent using this instance
PendingIntent? pendingIntent = PendingIntent.GetActivity(
    this, 0, notifyIntent, PendingIntentFlags.UpdateCurrent |
    PendingIntentFlags.Immutable);

// Setup the notification object with the PendingIntent
Notification notif = new NotificationCompat.Builder(this, /* notif id */)
    .SetContentIntent(pendingIntent) // make sure to set the pending intent
    .Build();

See this link for a working example project: https://github.com/SubverseIM/SubverseIM

Hope this helps!!

@sessiontalk
Copy link
Author

Thanks, we have already tried what you have done in fact using GetLaunchIntentForPackage of package manager "to replicate the behavior of launching the app from the home screen upon tapping the notification" improved things further but the bug still happens under certain sequences ie when swiping the app clear ,launching the app from an action button on notification and then after that closing the app and opening from a notification causes the exception. We cannot possibly handle every edge case ,the underlying issue needs fixing.

@IsaMorphic
Copy link

No problem! Fair enough, in my case I didn't have this edge case issue because my app uses a ForegroundService to stay resident in memory. I agree, it should be fixed at its source!
Cheers~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants