2010-06-30 37 views
23

開始時活動由Intent啓動,並且使用此Intent完成某些操作。如何清除啓動Activity的意圖?

當我更改我的Activity的方向時,它會再次重新加載,並將Intent傳遞給Activity。

如何清除該意圖以防止活動再次使用它?

我試過setIntent(null),但沒有結果。

+0

像素應該不接受明顯錯誤的答案並接受@ dcg的代替。那會爲我節省一個小時的工作路線。請參閱Dianne Hackborn關於setIntent是如何的帖子:https://groups.google.com/forum/#!msg/android-developers/vrLdM5mKeoY/ThkOonAbtloJ – JohnnyLambada 2015-02-03 21:26:36

+0

看看這是否有用 - stackoverflow.com/a/19820057/313042 – rajath 2017-09-06 07:12:42

回答

17

我有類似的問題。這幫助了我。 也許你還必須使用onSaveInstanceState(Bundle outState)並向bundle添加額外的數據,所以inState不爲空,不確定。

Intent activityIntent = null; // Subsequent times it's null 

@Override 
protected void onCreate(Bundle inState) { 
    super.onCreate(inState); 
    . 
    . 
    if (inState!=null) { 
     /*Recreating activity with a saved state*/ 
    } else { 
     /*Creating activity*/ 
     activityIntent = getIntent(); // First time through intent is used 
     /*Get your Extra Intent data here. You will be capturing data on 1st creation. */ 
} 
+0

我在下面有一個類似的方法,也可以讓你從最初的意圖中保留選擇性參數。 – dell116 2017-02-26 03:56:02

+1

從退出應用程序退出後從最近的應用程序列表中打開時,這對我無效。該應用程序將打開一個空包,但前一個意圖。 – CACuzcatlan 2017-07-14 20:24:37

0

我的建議是用布爾變量切換來檢查你的活動是否是第一次創建的。

否則,您可以查看您的onCreate方法,afaik僅在第一次創建活動時執行。

+1

同意。使用'onRetainNonConfigurationInstance()'將狀態從舊活動傳遞給新活動。 – CommonsWare 2010-06-30 11:45:17

+3

實際上,當屏幕方向改變時,onCreate()也會被調用(應用程序基本上被銷燬並在此期間重新創建)。 – 2010-07-01 08:09:14

1

如果你的意圖被髮送到你的活動與動作(帶的setAction),只是下面當您收到的意圖,以避免這種意圖的多重處理時,屏幕旋轉:

Intent i = getIntent(); 
i.setAction(null); 
setIntent(i); 
+0

不起作用 - 用api 25檢查。 – 2017-10-06 19:38:24

8

不要使用setIntent(null)。看起來雖然它有時可能有效,但在某些情況下,最初的意圖可能會回來。

取而代之的是,調用 setIntent()的簡單目的是沒有動作,數據或額外信息,例如 new Intent(Context, Class)

事實上,使用setIntent()其實並不是一個好的API設計決定。相反,正如dcg指出的那樣,確保您只在第一次檢查您的意圖時,savedInstanceState仍爲空。

+2

setIntent就在那裏讓你感到困惑 - 不要使用它。請參閱@ dcg的答案。當一個Activity被銷燬時,無論你使用setIntent做什麼,都會重新使用啓動該Activity的原始意圖。原始的Intent完全保留在應用程序之外,在ActivityManager中,setIntent對它沒有影響。請參閱Dianne Hackborn的帖子:https://groups.google.com/forum/#!msg/android-developers/vrLdM5mKeoY/ThkOonAbtloJ – JohnnyLambada 2015-02-03 21:23:58

+0

感謝您對該帖子的引用@JohnnyLambada!更新答案以避免誤導人們。 – 2015-02-03 21:50:05

+0

檢查'savedInstanceState == null'是否是BRILLIANT !!!這有助於區分我是從啓動器啓動MainActivity,還是通過打開電子郵件附件獲得收到的意圖。 +1 – 2015-07-15 21:10:20

0

在我來說,我需要將數據設置爲null:

getIntent().setData(null); 
2

老帖子,但有些人可以使用它。

不要浪費時間,如果你的應用程序恢復,意圖將再次出現。

使用開始一心想「活動繼續」,只是增加一個額外的價值

putExtra("DONE", 0) 

和各個應用程序恢復時間,檢查是否已經有這個值:

hasExtra("DONE") 

容易

+0

這不起作用。我認爲Android以不同的方式保存'Intent',而不是內存。你只是在運行時添加這個值。當活動被殺死後,系統再次檢索原始的「意圖」。 – 2018-01-22 02:25:30

0

intent.putExtra(YourKey,""); //reset the value to knknown setIntent(intent);

0

即使在手動清除Int ent和Intent extras在它們被解析後,似乎Activity.getIntent()將始終返回啓動Activity的原始Intent。

要解決這個問題,我建議是這樣的:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // The Intent provided by getIntent() (and its extras) will persist through a restore 
    // via savedInstance. Because of this, restoring this activity from a 
    // an instance that was originally started with extras (deep-link or 
    // pre-defined destination) may cause un-desired behavior 
    // (ie...infinite loop of sending the user directly to somewhere else because of a 
    // pre-defined alternate destination in the Intent's extras). 
    // 
    // To get around this, if restoring from savedInstanceState, we explicitly 
    // set a new Intent *** to override the original Intent that started the activity.*** 
    // Note...it is still possible to re-use the original Intent values...simply 
    // set them in the savedInstanceState Bundle in onSavedInstanceState. 
    if (savedInstanceState != null) { 
     // Place savedInstanceState Bundle as the Intent "extras" 
     setIntent(new Intent().putExtras(savedInstanceState)); 
    } 

    processIntent(getIntent()) 
} 

private void processIntent(Intent intent) { 
    if (getIntent().getExtras() == null) { 
     // Protection condition 
     return; 
    } 

    doSomething(intent.getExtras.getString("SOMETHING_I_REALLY_NEED_TO_PERSIST")); 

    final String somethingIDontWantToPersist = 
     intent.getExtras.getString("SOMETHING_I_DONT_WANT_TO_PERSIST"); 

    if(somethingIDontWantToPersist != null) { 
     doSomething(somethingIDontWantToPersist); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    // Save selective extras from original Intent... 
    savedInstanceState.putString("SOMETHING_I_REALLY_NEED_TO_PERSIST", "persistedValued"); 
    super.onSaveInstanceState(savedInstanceState); 
} 

這種方式,有傾倒的初衷,同時仍可以明確地保留原來的意圖/意向的某些部分的能力的機制臨時演員。

請注意,我沒有測試過所有的Activity啓動模式。

0

如果你設置的意圖動作,你可以用getIntent().setAction("");

例如在onCreate(...)清除:

... 
String action = getIntent().getAction(); 
if (action != null) { 
    if (action.equals(YOUR_ACTION_WHATEVER)) { 
    doSomethingHere(); // do something here 
    getIntent().setAction(""); // clear the action 
    } 
} 
... 

這將清除行動,否則它會被稱爲每次旋轉設備時。