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: Tapping a notification causes second OnCreate() call and crash. #14924

Open
RobTF opened this issue May 4, 2023 · 18 comments
Open

Android: Tapping a notification causes second OnCreate() call and crash. #14924

RobTF opened this issue May 4, 2023 · 18 comments
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@RobTF
Copy link

RobTF commented May 4, 2023

Description

I start with an application which generates a notification. I opted to use a foreground service to generate the notification however it doesn't seem to make a difference how the notification is generated.

The notification in the foregroun service is created like so;

                var notification = new NotificationCompat.Builder(this, "MyChannel")
                .SetContentTitle("Notification Title")
                .SetContentText("Notification Description")
                .SetSmallIcon(Resource.Drawable.DefaultNotificationIcon)
                .SetContentIntent(pendingIntent)
                .SetOngoing(true)
                .SetShowWhen(false)
                .Build();

                StartForeground(10000, notification);

I start the service in the OnCreate lifecycle callback, which results in an app with the notification present;
image
image

When the user taps on the notification two things happen;

1l The OnCreate lifecycle activity is called again for the same activity instance it got called for on startup. There is no call on OnDestroy.

  1. The app crashes with the following error
    image

Steps to Reproduce

  1. Open my reproduction project.
  2. Build and run on Android (real device or simulator)
  3. When the app launches wait for the notification to appear, and tap it.
  4. Observe crash.

Link to public reproduction project repository

https://github.com/RobTF/mauiplay/tree/master/NotificationTapIssue

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No

Relevant log output

[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: com.companyname.notificationtapissue, PID: 5211
[AndroidRuntime] java.lang.IllegalStateException: View with id -1: crc640ec207abc449b2ca.ShellPageContainer#onMeasure() did not set the measured dimension by calling setMeasuredDimension()
[AndroidRuntime] 	at android.view.View.measure(View.java:26370)
[AndroidRuntime] 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6981)
[AndroidRuntime] 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
[AndroidRuntime] 	at android.view.View.measure(View.java:26357)
[AndroidRuntime] 	at androidx.recyclerview.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:9681)
[AndroidRuntime] 	at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1657)
[AndroidRuntime] 	at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
[AndroidRuntime] 	at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
[AndroidRuntime] 	at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
[AndroidRuntime] 	at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
[AndroidRuntime] 	at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:149)
[AndroidRuntime] 	at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43)
[AndroidRuntime] 	at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:2253)
[AndroidRuntime] 	at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:953)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
[AndroidRuntime] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891)
[AndroidRuntime] 	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1729)
[AndroidRuntime] 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1638)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
[AndroidRuntime] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1263)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891)
[AndroidRuntime] 	at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1880)
[AndroidRuntime] 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1640)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
[AndroidRuntime] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
[AndroidRuntime] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
[AndroidRuntime] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891)
[AndroidRuntime] 	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1729)
[AndroidRuntime] 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1638)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
[AndroidRuntime] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
[AndroidRuntime] 	at com.android.internal.policy.DecorView.onLayout(DecorView.java:799)
[AndroidRuntime] 	at android.view.View.layout(View.java:23693)
[AndroidRuntime] 	at android.view.ViewGroup.layout(ViewGroup.java:6413)
[AndroidRuntime] 	at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3999)
[AndroidRuntime] 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3372)
[AndroidRuntime] 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2328)
[AndroidRuntime] 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9087)
[AndroidRuntime] 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231)
[AndroidRuntime] 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239)
[AndroidRuntime] 	at android.view.Choreographer.doCallbacks(Choreographer.java:899)
[AndroidRuntime] 	at android.view.Choreographer.doFrame(Choreographer.java:832)
[AndroidRuntime] 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214)
[AndroidRuntime] 	at android.os.Handler.handleCallback(Handler.java:942)
[AndroidRuntime] 	at android.os.Handler.dispatchMessage(Handler.java:99)
[AndroidRuntime] 	at android.os.Looper.loopOnce(Looper.java:201)
[AndroidRuntime] 	at android.os.Looper.loop(Looper.java:288)
[AndroidRuntime] 	at android.app.ActivityThread.main(ActivityThread.java:7872)
[AndroidRuntime] 	at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
**Java.Lang.IllegalStateException:** 'View with id -1: crc640ec207abc449b2ca.ShellPageContainer#onMeasure() did not set the measured dimension by calling setMeasuredDimension()'
@RobTF RobTF added the t/bug Something isn't working label May 4, 2023
@mattleibow mattleibow added platform/android 🤖 area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events area-controls-shell Shell Navigation, Routes, Tabs, Flyout labels May 4, 2023
@mattleibow mattleibow added this to the Backlog milestone May 4, 2023
@ghost
Copy link

ghost commented May 4, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@AmrAlSayed0
Copy link
Contributor

I think putting [Android.App.Activity(LaunchMode = Android.Content.PM.SingleTask)] or [Android.App.Activity(LaunchMode = Android.Content.PM.SingleInstance)] on MauiAppCompatActivity would solve the problem but is that what MAUI wants to do for ALL apps?

@RobTF
Copy link
Author

RobTF commented May 5, 2023

[Android.App.Activity(LaunchMode = Android.Content.PM.SingleTask)]

Thanks @AmrAlSayed0 - unfortunately this still fails in exactly the same way if the user taps the notification and the activity is in the foreground already, but does fix the issue of tapping when the activity is in the background.

EDIT: To clarify, when I say "this" doesn't work, I mean either of the proposed values for LaunchMode as I tested both.

@henda79
Copy link

henda79 commented May 11, 2023

Same issue here, I'm using Notifications from the Shiny.org lib

@ne0rrmatrix
Copy link

I am having same issue. Anyone have a workaround for this?

@masterofdaemon
Copy link

Me too

@ne0rrmatrix
Copy link

I think putting [Android.App.Activity(LaunchMode = Android.Content.PM.SingleTask)] or [Android.App.Activity(LaunchMode = Android.Content.PM.SingleInstance)] on MauiAppCompatActivity would solve the problem but is that what MAUI wants to do for ALL apps?

Changing to LaunchMode = Android.Content.PM.SingleTask) fixed it for me. But I would like to see at least a way forward that does not require this adjustment.

@RobTF
Copy link
Author

RobTF commented Jun 20, 2023

I think putting [Android.App.Activity(LaunchMode = Android.Content.PM.SingleTask)] or [Android.App.Activity(LaunchMode = Android.Content.PM.SingleInstance)] on MauiAppCompatActivity would solve the problem but is that what MAUI wants to do for ALL apps?

Changing to LaunchMode = Android.Content.PM.SingleTask) fixed it for me. But I would like to see at least a way forward that does not require this adjustment.

This only works if killing the activity kills the app process as essentially when you tap the notification you're getting a totally fresh copy of your app. If you run foreground services etc. unfortunately, it has no effect and the crash still occurs as your app has an existing process running.

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 6, 2023
@XamlTest
Copy link
Collaborator

XamlTest commented Jul 6, 2023

Verified this on Visual Studio Enterprise 17.7.0 Preview 2.0. Repro on Android 13.0-API33 with below Project:
NotificationTapIssue.zip

@sfernandez131
Copy link

Still a problem.

@sfernandez131
Copy link

It appears to be crashing somewhere in the base.OnStart() method. I can't figure out how to debug or at least see what's going on.

@ibanezjp
Copy link

ibanezjp commented Jan 5, 2024

Still a big issue. We are using .NET 8 and this is preventing us from releasing a version. Any update?

@AlleSchonWeg
Copy link

Hi,
in this video: https://youtu.be/YNkdsJTyOJE?t=64 the author has the same exception and show how to fix it.

@RobTF
Copy link
Author

RobTF commented Jan 5, 2024

Unfortunately, the solution in the video doesn't work if you kill the activity via a "swipe up" but the app contains background processing such as a foreground service (see my note above).

The problem remains and cannot be worked around by adjusting the activity type.

@edwinjoelmarte
Copy link

Solución:
image

@hugofonseca-incontrol
Copy link

Solución: image

@edwinjoelmarte could you please provide more detail regarding where and when in the lifecycle of the app are you doing this exactly to overcome the issue?

@sfernandez131
Copy link

That's assigned to the intent that's captured in the notification.

@konradzuse
Copy link

This is a problem for me also, using Shiny.Notifications library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests