【发布时间】:2019-12-04 13:16:05
【问题描述】:
如果我有一个应用程序有
A -> B -> C -> D
在堆栈中。我怎样才能从通知中打开一个活动并使堆栈成为
A -> B -> C -> D -> E
如果应用程序被终止,我如何从通知中打开应用程序,例如从通知中打开 E 活动,但在再次打开应用程序时阻止它启动 [例如,我打开 E 活动然后我回退以退出应用程序,然后当我打开再次应用我希望它打开 A 活动(根活动)而不是 E 活动。]
// When Open Application if app is terminated.
val intent = Intent(activity!!, SampleActivity::class.java)
val pendingIntent= PendingIntent.getActivity(activity!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
with(NotificationManagerCompat.from(activity!!)) {
notify(
java.lang.System.currentTimeMillis().toInt(), notiBuilder
.setContentTitle("Title")
.setContentIntent(pendingIntent)
.setContentText("body")
.setNumber(1)
.setSmallIcon(R.drawable.notification_icon_background)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build())
}
// When Open Application if app is not terminated.
val intent = Intent(activity!!, HomeActivity::class.java)
intent.putExtra("FurtherActivity", 1)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val pendingIntent= PendingIntent.getActivity(activity!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
with(NotificationManagerCompat.from(activity!!)) {
notify(
java.lang.System.currentTimeMillis().toInt(), notiBuilder
.setContentTitle("Title")
.setContentIntent(pendingIntent)
.setContentText("body")
.setNumber(1)
.setSmallIcon(R.drawable.notification_icon_background)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build())
}
// HomeActivity
override fun onCreate() {
......
......
......
if(intent.getIntExtra("FurtherActivity", 0) == 1) {
SampleActivity.start(this@HomeActivity)
viewpager.currentItem = 0
tab_layout.getTabAt(0)!!.select()
}
......
......
}
override fun onNewIntent(newIntent: Intent?) {
super.onNewIntent(Intent())
if(intent.getIntExtra("FurtherActivity", 0) == 1) {
SampleActivity.start(this@HomeActivity)
viewpager.currentItem = 0
tab_layout.getTabAt(0)!!.select()
}
}
【问题讨论】:
-
如果您想增加问题被回答的机会,我建议您包含一段示例代码来演示您的问题,以便回答的人可以提出更正建议,而不是从头开始编写所有内容。
-
@IcedLance 感谢提醒,我忘了添加我的代码
标签: android android-intent android-activity kotlin android-notifications