【问题标题】:Android: Activity is using old intent if launching app from Recent TaskAndroid:如果从最近的任务启动应用程序,活动正在使用旧意图
【发布时间】:2012-10-30 08:56:52
【问题描述】:

我正在实施 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,然后我再次从 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 屏幕,并带有“B-notification-intent”。相反,我想要最新的 Intent,即“B-A-intent”。我在B 中使用此代码:

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

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

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

【问题讨论】:

  • 尝试将其更改为 on resume protected void onResume(Bundle b) { fetchDataFromDB(getIntent()); }
  • @droidhot 谢谢,你解决了我的问题
  • @droidhot 请解释你的答案(我做错了什么)并将其发布在下面的答案块中
  • 当你按下主页按钮时,Activity 进入暂停状态,当它返回时,会调用 resume,并且不会调用 start,因此数据将是 on 时的意图创建在您的情况下是通知意图引用活动生命周期以更好地理解它
  • 点击通知后不刷新??无论如何,这不是恢复的问题(请检查活动生命周期),您可以创建一个函数或接收器来解决问题——(我希望如果你点击它显示刷新的屏幕的通知)——如果你注意到facebook 应用程序它不会用新的推送刷新当前屏幕,但你必须拉刷新--

标签: android android-intent notifications flags


【解决方案1】:

我还注意到,有时 ActivityonCreate() 在从“最近”启动时会变得陈旧 Intents,但一种检查方法,以便您可以处理Intent 适当。

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

这个标志通常不是由应用程序代码设置的,但如果这个活动是从历史中启动的(长按主页键),系统会为你设置。

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

【讨论】:

  • 这在启动器活动也收到意图时非常有用。
  • 我为此浪费了半天时间。很有用。
  • 该死,我找了 336592896 两天了,这个意图标志是什么。感谢您的回答。
  • 像魅力一样工作!仅供参考,我们在这里使用 JAVA & 位运算符。我已经编辑了答案以添加代码的 Kotlin 版本。
【解决方案2】:

由于某种原因,bkDJ's / the accepted answer 对我不起作用。仅当接受的答案也对您不起作用时,请尝试以下操作:

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

我必须为发生此问题的每个意图添加一个时间戳。并且仅在时间戳不超过 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多