Android全局退出的两种方法
第一种方法参考《第一行代码》78页
建立一个ActivityCollector类,提供静态方法addActivity,fininshAll(以list为容器)
然后我们建立的Activity都继承一个BaseActivity,在BaseActivity中的oncreate方法中,使用ActivityCollector的addActivity(this),
想要随时全局退出只要调用ActivityCollector.finishAll()
今天在看到另一个和Activity启动模式有关的全局退出方法

我们写了三个Activity,然后设置textView的点击事件,跳转下一个Activity,这时候我们想在第二个或第三个Activity直接退出,该怎么做
第一步:设置MainActivity的 android:launchMode="singleTask"
singleTask
- 判断Activity所需任务栈内是否已经存在,如果存在,就把该Activity切换到栈顶(会导致在它之上的都会出栈)
- 如果所需任务栈都不存在,就会先创建任务栈再创建该Activity
- 可以理解为 顶置Activity+singleTop 的模式
第二步:我们建立一个BackUtils类
import android.app.Activity;
import android.content.Intent; public class BackUtils {
public static void allBack(Activity activity){
Intent intent=new Intent(activity, MainActivity.class);
activity.startActivity(intent);
}
}
类中的AllBack方法就是去启动MainActivity。
我们可以在其他Activity中复写
@Override
public void onBackPressed() {
super.onBackPressed();
BackUtils.allBack(this);
}
这时候,可以思考到,MainActivity是singletask,这样来到MainActivy,栈中MainActivy上面的都会出栈,但是这时候还剩下一个MainActivity,
我们做的就是要来到MainActivity后立即销毁它。
这时,刚好存在方法
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
finish();
}
对于这个方法
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent.FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity. In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
An activity will always be paused before receiving a new intent, so you can count on onResume being called after this method.
这里的onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.我们的MainActivity使我们进去App的第一个界面,所以必然已经存在,当设置singleTop或者singleTask时,会调用onNewIntent()。
我们只要在onNewIntent()调用finish。
Android全局退出的两种方法的更多相关文章
- Android 抗锯齿的两种方法
Android 抗锯齿的两种方法 (其一:paint.setAntiAlias(ture);paint.setBitmapFilter(true)) 在Android中,目前,我知道有两种出现锯齿 ...
- android emulator启动的两种方法详解
android emulator启动的两种方法详解 转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...
- android使用全局变量的两种方法
在我们使用android编写程序的时候,少不了想利用全局变量,但是面向对象语言和过程语言区别很大,不再是include就可以的.这里我写了使用全局变量的两种方法: 1.使用applicati ...
- iOS 关于退出键盘两种方法和避免遮挡
退出键盘: 方法1:不使用代理,直接使用: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.textFi ...
- [转]android使用全局变量的两种方法
本文转自:http://blog.csdn.net/feiyangxiaomi/article/details/9966215 在我们使用android编写程序的时候,少不了想利用全局变量,但是面向对 ...
- Android访问WebService的两种方法
首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...
- (转)Android创建桌面快捷方式两种方法
[IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...
- Android apk签名的两种方法
编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 为了保证 ...
- android 实现分享功能两种方法
当我想做一个智能的记事本的时候,我就在尝试自己写一组分享功能.后来才知道,原来每个社交软件中都有自己的分享接口. 这就大大减少了我们的代码量了. 第一种方法:特点--简单 package com.ex ...
随机推荐
- The in operator
The operators we have seen so far are all special characters like + and *, but there are a few opera ...
- Codeforces 708D 费用流 (呃我们的考试题)
NB的题目背景 输入输出一样 考试的时候貌似只有gzz一个人搞出来了 %gzz 思路: 分情况讨论 add(x,y,C,E) C是费用 E是流量 1. f>c add(x,y,2,inf),ad ...
- 关于ListView的一些不常用到的属性
1.setCacheColorHint自定义listview的时候,当你不使用Android:cacheColorHint=“#00000000”会出现下面选中一个空间黑色底色的情况,破坏整体美观度 ...
- Ambari Confirm Hosts Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).解决 Permanently added 'hdp21,192. ...
- pc端如何引用日期插件
页面的html部分 <li> <span>出生日期</span> <input type="text" placeholder=" ...
- 装了ubuntu后笔记本电脑的无线网卡用不了,怎么设置?
百度经验的一篇文章 http://jingyan.baidu.com/article/ca2d939dd4f1b4eb6c31ce09.html 点击右上角的齿轮,选择“系统设置” 点击“软件和更 ...
- etTimeout来实现setInterval
etTimeout来实现setInterval <script type="text/javascript"> function interval(func, w, t ...
- python的开发工具UliPad安装篇
之前文章里写过一个搭建windows下搭建Selenium+Eclipse+Python环境,如今认为这个Eclipse太大了,太笨重了,重新启动又慢,像Python脚本轻级语言,不是必需用那么大的工 ...
- POJ 3225 Help with Intervals(线段树)
POJ 3225 Help with Intervals 题目链接 集合数字有的为1,没有为0,那么几种操作相应就是置为0或置为1或者翻转,这个随便推推就能够了,然后开闭区间的处理方式就是把区间扩大成 ...
- Android禁止ViewPager的左右滑动
转载请注明出处:http://blog.csdn.net/allen315410/article/details/40744287 有时候在开发中会遇到一些"诡异"的要求,比方在V ...