Intent.FLAG_ACTIVITY_CLEAR_TOP 的使用注意

最近开发一个下载的应用,然后有很多层跳转关系,跳到最后进行下载,下载完毕之后弹出一个按钮,点击之后会将所有activity都结束掉。一开始用的方法是

 

						Intent intent = new Intent(DownLoad.this,
								NetworkUpdate.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						intent.putExtra("exit", true);
						startActivity(intent);

 然后在NetworkUpdate.class的oncreate中判断extra的exit属性,是true则finish自己。根据以往的经验设置了Intent.FLAG_ACTIVITY_CLEAR_TOP之后应该会将除了目标activity之上的所有activity全部结束才对,在下载小文件的时候上述功能也确实凑效了。但是问题来了。。当下载的文件特别大,耗时特别长的时候,上述动作执行会没效果!按钮点了又点没任何反应。。。
开始就想,跟下载文件耗时长不长有什么关系呢?后来发现了API文档:

public static final int FLAG_ACTIVITY_CLEAR_TOP

API level 1
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已经被安卓自己干掉了(猜测)。

最后更换了另一个实现方法。

Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK

 

直接这两个标志一起用就好了~连exit都不需要设置啦。

posted @ 2014-09-18 11:16  不想换名了  阅读(979)  评论(0编辑  收藏  举报