簡體   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