Android之用Intent.FLAG_ACTIVITY_CLEAR_TOP解决界面重复拉起问题
2016-06-29 22:40
447 查看
今天遇到了一个问题:
就是界面在服务拉起来用的Intent.FLAG_ACTIVITY_NEW_TASK导致登录界面拉起很多次,登录还要登录,主要是之前的activity没有关闭
用it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP)解决。
A - B - C
当 B - A - B 跳转的时候,使用Intent的FLAG_ACTIVITY_CLEAR_TOP会让第一个B和第二个A,destory掉
但是当B - A - C跳转的时候不会调用B和A的destory
其实这个问题以前遇到过,今天遇到的时候又忘记了。查看API文档才发现原因,所以这里记录一下避免下次又忘记了:
Added in 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。
所以上面当A跳C的时候不满足此条件。
当然如果你想实现这个效果可以使用:
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
不过不幸的是,此方法要求最低API为11
就是界面在服务拉起来用的Intent.FLAG_ACTIVITY_NEW_TASK导致登录界面拉起很多次,登录还要登录,主要是之前的activity没有关闭
用it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP)解决。
A - B - C
当 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 1If 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
相关文章推荐
- Android绘图机制(一)
- 使用FlowDroid生成Android应用程序的函数调用图
- Android中的RSA加密
- Activity的onTrimMemory()方法
- 自定义View-打造属于你的炫酷按钮
- android的support包有什么?
- [Android]第一个cm调试分析
- Android总结
- android笔记:ListView及ArrayAdapter
- android 动画(1) 补间动画
- android项目中实现百度地图中点聚合功能
- 重新认识View(二)
- Android NDK 简介
- Android 之 IPC 进程通信全解析 -- 墙裂推荐
- Android 之 IPC 进程通信全解析 -- 墙裂推荐
- Android 自定义View (前置学习) -- Paint对象 Color对象 Canvas对象
- android xml属性
- 重新认识View(一)
- activity的启动模式
- Android的Activity中的Menu使用总结