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都不需要设置啦。

转载于:https://www.cnblogs.com/blairsProgrammer/p/3978817.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值