繁体   English   中英

Android:如果从“近期任务”启动应用程序,则 Activity 使用旧意图

[英]Android: Activity is using old intent if launching app from Recent Task

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

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 打开 Activity B ,比如“B-notification-intent”,然后我使用 Back 按钮从B打开 Activity A ,然后我再次从具有新 Intent 的A启动B (比如“BA-intent”)。 我使用下面的代码:

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屏幕。 相反,我想要最新的意图,即“BA 意图”。 我在B使用此代码:

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

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

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

我还注意到,有时一个ActivityonCreate()是越来越陈旧Intent期从最近通话启动时,但一种方法来检查,好让你们能处理Intent适当。

爪哇:

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 设备具有用于最近的专用按钮的事实:

此标志通常不是由应用程序代码设置的,而是由系统为您设置的,如果此活动是从历史记录中启动的(长按主页键)。

(注意,我意识到您几年前以另一种方式解决了您的问题,但此问题是“最近 android 旧意图”的热门搜索结果之一,其他相关问题均未提及此标志,因此希望此答案可以帮助其他人.)

出于某种原因, bkDJ/接受的答案对我不起作用。 仅当接受的答案也不适合您时,请尝试以下操作:

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 或在第一次处理意图后为其设置布尔值都不会阻止意图被重用。

我必须为发生此问题的每个意图添加时间戳。 并且仅当时间戳不超过 30 秒时才执行意图。

像这样:

Intent intent = new Intent(CONTEXT, TargetActivity.class);
Calendar now = Calendar.getInstance();
intent.putExtra("timestampOfIntentInMilliseconds", now.getTimeInMillis());
// put your actual extras to the intent 

然后在处理意图时:

Bundle extras = intent.getExtras();
long timestampOfIntentInMilliseconds = extras.getLong("timestampOfIntentInMilliseconds");
now = Calendar.getInstance();
if(now.getTimeInMillis() < (timestampOfIntentInMilliseconds + 30000))
{
   // do what you want to do with the intent
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM