使用时的PendingIntent和singleTask不singleInstance尊重? [英] singleTask and singleInstance not respected when using PendingIntent?
问题描述
我有launchMode活动设置为singleTask:
I have an activity with launchMode set to singleTask:
<activity
android:name="com.blah.blah.MyActivity"
android:launchMode="singleTask">
</activity>
我有一个的PendingIntent正在进行的通知,启动这项活动:
I have an ongoing notification with a PendingIntent that launches that activity:
Intent activityIntent = new Intent( this,
MyActivity.class );
TaskStackBuilder stackBuilder = TaskStackBuilder.create( this );
stackBuilder.addParentStack( MyActivity.class );
stackBuilder.addNextIntent( activityIntent );
PendingIntent resultingActivityPendingIntent = stackBuilder.getPendingIntent( REQUEST_CODE,
PendingIntent.FLAG_UPDATE_CURRENT );
...
m_notificationBuilder.setContentIntent( resultingActivityPendingIntent );
startForeground( ONGOING_NOTIFICATION_ID,
m_notificationBuilder.build() );
当我与现有MyActivity互动,然后我打主页,并通过发射器,MyActivity的 onNewIntent()
如预期被调用。
When I am interacting with an existing MyActivity, then I hit Home and restart MyActivity via the launcher, MyActivity's onNewIntent()
gets called as expected.
问题是,当我与现有MyActivity互动,我点击正在进行的通知,新的MyActivity通过的onCreate()
,以及现有的创建一种是通过破坏的onDestroy()
。我预计,MyActivity的 onNewIntent()
来代替将被调用。为什么不这样呢?
The problem is that when I am interacting with an existing MyActivity, and I click on the ongoing notification, a new MyActivity is created via onCreate()
, and the existing one is destroyed via onDestroy()
. I expected that MyActivity's onNewIntent()
would be called instead. Why doesn't this happen?
我曾尝试这些答案没有成功:
I have tried these answers without success:
- Intent - 如果活动正在运行,把它前面,否则启动一个新的(从通知)
- How作出通知的简历和无法重新创建活动?
- FLAG_ACTIVITY_REORDER_TO_FRONT忽略(在这种情况下,在用户点击通知时现有的实例不被破坏)
- Intent - if activity is running, bring it to front, else start a new one (from notification)
- How to make notification resume and not recreate activity?
- FLAG_ACTIVITY_REORDER_TO_FRONT ignored (in this case the existing instance is not destroyed when the notification is clicked)
推荐答案
您的问题是由于使用了 TaskStackBuilder
的。这code是造成你的问题:
Your problem is due to the use of TaskStackBuilder
. This code is causing your problem:
Intent activityIntent = new Intent( this,
MyActivity.class );
TaskStackBuilder stackBuilder = TaskStackBuilder.create( this );
stackBuilder.addParentStack( MyActivity.class );
stackBuilder.addNextIntent( activityIntent );
PendingIntent resultingActivityPendingIntent =
stackBuilder.getPendingIntent(REQUEST_CODE,
PendingIntent.FLAG_UPDATE_CURRENT );
当您使用 TaskStackBuilder
这样一来,它设置其他标志中生成的意图
S中的导致任务复位(在任务活动被破坏)的活动被推出了。
When you use TaskStackBuilder
this way, it sets additional flags in the generated Intent
s that cause the task to be reset (all Activities in the task are destroyed) before your Activity gets launched.
而是使用:
Intent activityIntent = new Intent( this,
MyActivity.class );
PendingIntent resultingActivityPendingIntent =
PendingIntent.getActivity(this, REQUEST_CODE,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT );
这篇关于使用时的PendingIntent和singleTask不singleInstance尊重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!