使用 PendingIntent 时不考虑 singleTask 和 singleInstance?
singleTask and singleInstance not respected when using PendingIntent?
我有一个 activity 的 launchMode 设置为 singleTask:
<activity
android:name="com.blah.blah.MyActivity"
android:launchMode="singleTask">
</activity>
我有一个正在启动的 PendingIntent 通知 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,MyActivity 的 onNewIntent()
按预期被调用。
问题是,当我与现有的 MyActivity 交互时,我单击正在进行的通知,通过 onCreate()
创建了一个新的 MyActivity,并通过 onDestroy()
销毁了现有的 MyActivity .我预计 MyActivity 的 onNewIntent()
会被调用。为什么这没有发生?
我试过这些答案都没有成功:
- 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(在这种情况下,单击通知时不会销毁现有实例)
您的问题是由于使用了 TaskStackBuilder
。此代码导致了您的问题:
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
时,它会在生成的 Intent
中设置额外的标志,导致在 [=21] 之前重置任务(任务中的所有 Activity 都被销毁) =] 启动。
改为使用:
Intent activityIntent = new Intent( this,
MyActivity.class );
PendingIntent resultingActivityPendingIntent =
PendingIntent.getActivity(this, REQUEST_CODE,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT );
我有一个 activity 的 launchMode 设置为 singleTask:
<activity
android:name="com.blah.blah.MyActivity"
android:launchMode="singleTask">
</activity>
我有一个正在启动的 PendingIntent 通知 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,MyActivity 的 onNewIntent()
按预期被调用。
问题是,当我与现有的 MyActivity 交互时,我单击正在进行的通知,通过 onCreate()
创建了一个新的 MyActivity,并通过 onDestroy()
销毁了现有的 MyActivity .我预计 MyActivity 的 onNewIntent()
会被调用。为什么这没有发生?
我试过这些答案都没有成功:
- 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(在这种情况下,单击通知时不会销毁现有实例)
您的问题是由于使用了 TaskStackBuilder
。此代码导致了您的问题:
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
时,它会在生成的 Intent
中设置额外的标志,导致在 [=21] 之前重置任务(任务中的所有 Activity 都被销毁) =] 启动。
改为使用:
Intent activityIntent = new Intent( this,
MyActivity.class );
PendingIntent resultingActivityPendingIntent =
PendingIntent.getActivity(this, REQUEST_CODE,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT );