问题
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