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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容