Android的Intent.FLAG_ACTIVITY_CLEAR_TOP無效

轉載:http://blog.csdn.net/u011361576/article/details/48626237

今天寫代碼遇到了一個問題:
當 B - A - B 跳轉的時候,使用Intent的FLAG_ACTIVITY_CLEAR_TOP會讓第一個B和第二個A,destory掉,但是當B - A - C跳轉的時候不會調用B和A的destory。
查看API文檔才發現原因,所以這里記錄一下避免下次忘記了:

public static final int FLAG_ACTIVITY_CLEAR_TOP
Added in [API level 1](http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

如果設置這個屬性,是當要啟動的Activity已經存在當前Task中,才會在啟動的時候銷毀其他的Activity。
所以上面當A跳C的時候不滿足此條件。

當然如果想實現這個效果可以使用:

it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);

不過此方法要求最低API為11

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容