Android:如果从“近期任务"启动应用程序,则活动正在使用旧的意图 [英] Android: Activity is using old intent if launching app from Recent Task

查看:264
本文介绍了Android:如果从“近期任务"启动应用程序,则活动正在使用旧的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing GCM. My app has two activities, say A and B. I'm using this code to launch B from the 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);

NotificationBar用一个Intent打开活动B,说'B-notification-intent',然后使用后退"按钮从B中打开Activity A,然后再次从A中启动B.新的意图(例如"BA意图").我使用以下代码:

NotificationBar opens Activity B with an Intent, say 'B-notification-intent', then I open Activity A from B using Back button, then again I launch B from A having a new Intent (say 'B-A-intent'). I use the code below:

intent = new Intent(this, B.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);              
startActivity(intent);

然后我在B中获得新数据(即B的屏幕被刷新). 但是,如果我按下主页"按钮,然后从最近使用的应用程序"启动该应用程序,则会出现带有"B-notification-intent"的较旧的B屏幕.相反,我需要最新的Intent,即"B-A-intent".我在B中使用此代码:

And then I get New Data in B (i.e the screen of B is refreshed). But if I press the Home button and then I launch the application from Recent app then I get older B screen with 'B-notification-intent'. Instead, I want the latest Intent, i.e 'B-A-intent'. I'm using this code in B:

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

@Override
protected void onNewIntent(Intent intent)
{
    fetchDataFromDB(intent);        
}

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

So anybody please help me getting my current screen (intent).

推荐答案

我还注意到,有时ActivityonCreate()从近期"新闻发布时会变得过时的Intent,但是这里的一种检查方法,以便您可以适当地处理Intent.

I also noticed that sometimes an Activity's onCreate() was getting stale Intents when launched from Recents, but there is a way to check for that so you can handle the Intent appropriately.

JAVA:

protected boolean wasLaunchedFromRecents() {
    return (getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
}

科特琳:

fun wasLaunchedFromRecents(): Boolean {
    val flags: Int = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
    return flags == Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
}

以我的拙见,该标志的名称不正确(其他引用近期活动"列表的标志实际上使用了该词,例如FLAG_ACTIVITY_EXCLUDE_FROM_RECENTSFLAG_ACTIVITY_RETAIN_IN_RECENTS),并且文档从未更新过以反映许多流行的Android设备具有以下事实:专用于最新消息"的按钮:

In my humble opinion, that flag is poorly named (other flags referencing the Recents list actually use that word, e.g. FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS, FLAG_ACTIVITY_RETAIN_IN_RECENTS) and the documentation was never updated to reflect the fact that many popular Android devices have a dedicated button for Recents:

此标志通常不是由应用程序代码设置的,而是由历史记录(长按主键)启动的,则由系统为您设置.

This flag is not normally set by application code, but set for you by the system if this activity is being launched from history (longpress home key).

(注意,我意识到您是在几年前解决您的问题的,但是这个问题是最近的android old intent"的热门搜索结果之一,其他相关问题都没有提及此标志,因此希望此答案可以帮助别人.)

(N.B. I realize that you solved your issue another way years ago, but this question is one of the top search results for 'android old intent recent' and none of the other related questions mention this flag, so hopefully this Answer can help someone else.)

这篇关于Android:如果从“近期任务"启动应用程序,则活动正在使用旧的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆