zoukankan      html  css  js  c++  java
  • 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都不需要设置啦。

  • 相关阅读:
    网络通讯协议的基本要素
    java实现二维码的生成与解析
    SpringCloud应用间通信-RestTemplate与Feign
    SpringCloud服务注册与发现-Eureka、Nacos和Consul
    极光推送-java消息推送app
    Git的回滚和撤销操作
    SOFABoot学习
    记录一次生产环境下EleasticSearch故障(cpu打满)
    记录SQL优化
    利用二进制存储多种状态
  • 原文地址:https://www.cnblogs.com/blairsProgrammer/p/3978817.html
Copyright © 2011-2022 走看看