我有一个活动,其launchMode设置为singleTask:
<activity
android:name="com.blah.blah.MyActivity"
android:launchMode="singleTask">
</activity>
我持续通知PendingIntent启动该活动:
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交互时,我点击Home并通过启动器重新启动MyActivity,MyActivity的onNewIntent()
会按预期调用。
问题在于,当我与现有的MyActivity进行交互时,如果我点击正在进行的通知,则会通过onCreate()
创建新的MyActivity,并通过onDestroy()
销毁现有的MyActivity。我预计会调用MyActivity的onNewIntent()
。为什么不这样呢?
我试过这些答案却没有成功:
答案 0 :(得分:7)
您的问题是由于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
中设置其他标志,这些标志会导致重置任务(任务中的所有活动都将被销毁),然后才能启动Activity。 / p>
改为使用:
Intent activityIntent = new Intent( this,
MyActivity.class );
PendingIntent resultingActivityPendingIntent =
PendingIntent.getActivity(this, REQUEST_CODE,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT );