Skip to content

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

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

Open
sessiontalk opened this issue Jan 10, 2025 · 8 comments · May be fixed by #18893

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
Contributor

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
Contributor

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~

@emmauss
Copy link
Contributor

emmauss commented Jan 14, 2025

Could you provide a minimal repro?

daniel-lerch added a commit to daniel-lerch/AvaloniaAndroidIOEDemo that referenced this issue May 16, 2025
@daniel-lerch
Copy link
Contributor

@emmauss I came accross the same issue and created a minimal reproducible example here. In contrast to this issue's description my example uses an intent-filter to show files instead of notifications. Nevertheless, I believe the underlying error is the same.

@emmauss
Copy link
Contributor

emmauss commented May 16, 2025

@daniel-lerch you can fix it by adding LaunchMode = LaunchMode.SingleInstance, to your activity options, but that would force your app into single instance mode. For multi-instancing or embedding scenerios, we would need changes to how lifecycle works on android.

@daniel-lerch
Copy link
Contributor

@emmauss Thanks for pointing this out. I would propose to make LaunchMode.SingleInstance the default in Avalonia templates and add to Avalonia's documentation that other launch modes are currently not supported due to its architecture.

Regarding the bugs that @sessiontalk originally described, I cannot reproduce them when using LaunchMode.SingleInstance. Regardless whether I use notifications or file open intents, swipe out the app or not, it simply does not crash. But that is actually what I would expect as Android documentation says: Also, the device can hold only one instance of the "singleInstance" activity at a time Link.

@emmauss
Copy link
Contributor

emmauss commented May 17, 2025

There are many reasons you wouldn't want to have SingleInstance as default. It isn't the default mode for an android app. And it can cause app state to be lost. Take a scenario where you have an active activity thats viewing a file. You open a new file in your file explorer. That replaces your previous file data. Pressing back to go to your file browser will finish the activity and remove the whole app from your tasks.
We instead need to have a lifecycle where you can open as many activities and not interfere with each activity state.

@emmauss emmauss linked a pull request May 20, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants