Opening notification from lock screen is behaving differently than opening the notification from the notification bar

做~自己de王妃 提交于 2019-12-23 12:29:16

问题


I am facing a strange issue when opening a notification through lock screen. Here is the structure of my application

  • MainActivity - The opening activity of the app.
  • NotificationActivity - The activity opened when the user clicks on a notification.

Here is how I am creating the notification

val mBuilder = NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon_background)
            .setContentTitle("Title")
            .setContentText("Content")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

val targetIntent = Intent(this, NotificationActivity::class.java)
val pendingContentIntent = PendingIntent.getActivity(
            this, 10, targetIntent,
            PendingIntent.FLAG_CANCEL_CURRENT
        )
mBuilder.setContentIntent(pendingContentIntent)
val notificationManager = NotificationManagerCompat.from(this)
notificationManager.notify(id, mBuilder.build())

Now if I am in MainActivity and press the home button, and then I lock my device. After clicking the notification from the lock screen, NotificationActivity is opening, but onResume() method of MainActivity is also called.

Here are some of the logs I took out after using the command

adb logcat -b events

04-27 12:13:14.360  1445  8699 I am_focused_stack: [0,1,5,moveTaskToFront]
04-27 12:13:14.360  1445  8699 I wm_task_moved: [840,1,16]
04-27 12:13:14.364  1445  8699 I am_pause_activity: [0,224827670,com.android.systemui/.recents.RecentsActivity]
04-27 12:13:14.366  1445  8699 I am_task_to_front: [0,840]
04-27 12:13:14.386  3189  3189 I am_on_paused_called: [0,com.android.systemui.recents.RecentsActivity,handlePauseActivity]
04-27 12:13:14.388  1445  8699 I am_set_resumed_activity: [0,com.packagename/com.activity.MainActivity,resumeTopActivityInnerLocked]

But if I opening the same notification through notification bar, then onResume() method of MainActivity does not get called. Ideally it should not be called from lock screen also, because I am only passing intent for NotificationActivity

Is there any way I can avoid calling of onResume() method of MainActivity

PS- I am not sure that the issue is happening across all devices and all OS versions. I tested in on my OnePlus5 device which has Android Oreo installed and one more Lenevo device which has Android 5 installed.

来源:https://stackoverflow.com/questions/50056911/opening-notification-from-lock-screen-is-behaving-differently-than-opening-the-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!