Android:如果从Recent Task启动应用程序,则Activity正在使用旧意图

Atu*_*waj 21 notifications flags android android-intent

我正在实施GCM.我的应用程序有两个活动,比如说AB.我正在使用此代码B从NotificationBar 启动:

long when = System.currentTimeMillis();
NotificationManager notificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = context.getString(R.string.app_name);        
Notification notification = new Notification(R.drawable.app_notification_icon, "De Centrale", when);//message
Intent notificationIntent = new Intent(context, B.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); //|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
Run Code Online (Sandbox Code Playgroud)

NotificationBar打开活动B,意图,说"B-通知意向",然后我打开活动AB使用后退按钮,然后重新启动我BA有一个新的intent(说"BA-意图").我使用下面的代码:

intent = new Intent(this, B.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);              
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

然后我获得新数据B(即B刷新的屏幕).但是,如果我按下主页按钮,然后我从最近的应用程序启动应用程序,那么我会B使用'B-notification-intent'来获得更旧的屏幕.相反,我想要最新的意图,即'BA-intent'.我正在使用此代码B:

@Override
protected void onCreate(Bundle b)
{
    fetchDataFromDB(getIntent());           
}

@Override
protected void onNewIntent(Intent intent)
{
    fetchDataFromDB(intent);        
}
Run Code Online (Sandbox Code Playgroud)

所以任何人请帮助我获取当前的屏幕(意图).

bkD*_*kDJ 27

我还注意到,有时一个ActivityonCreate()是越来越陈旧Intent期从最近通话启动时,但有一种方法来检查是否有这样你就可以处理Intent适当.

protected boolean wasLaunchedFromRecents() {
    return (getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
}
Run Code Online (Sandbox Code Playgroud)

在我的愚见,该标志名为很差(其它标志引用最近通话列表实际上用这个词,例如FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS,FLAG_ACTIVITY_RETAIN_IN_RECENTS)和文档从未更新,以反映一个事实,即许多流行的Android设备有一个最近通话专用按钮:

此标志通常不是由应用程序代码设置的,而是由系统为您设置的,如果从历史记录启动此活动(longpress home key).

(注意我意识到你几年前以另一种方式解决了你的问题,但这个问题是'android old intent recent'的最佳搜索结果之一,其他相关问题都没有提到这个标志,所以希望这个答案可以帮助别人.)