说明
在项目开发过程中,我们一定会遇到这样的需求,就是——一次性退出多个Activity。网上最常见的解决方案就是定义list集合,然后在onCreate()方法中将每个Activity添加到list集合中,在onDestroy()中去移除list集合中的Activity;或者就是广播或者其他的方案等等,今天我们就把这个问题的常用解决方案总结下,这个是我参照网上的解决方案自己汇总在一起,可能总结的不是很全,敬请谅解。解决方案
常见的解决方案有5种:堆栈管理器、list集合、广播、startActivityForResult()方法跳转然后用请求码和结果码判断、使用FLAG_ACTIVITY_CLEAR_TOP退出整个程序,就是这5种,其中常用的有堆栈管理器、list集合、广播,其余两种不太常用,那么下边我们就对这5种方法进行具体分析下:5种方法具体分析
3.1>:通过堆栈管理器去管理
定义一个堆栈管理器,写成单例模式,在里边new Stack()栈,在每个Activity的onCreate()中将对应Activity添加到栈,在最后一个Activity的点击去移除栈中activity,然后 将activity置为null即可,具体代码如下:
/**
* Email 2185134304@qq.com
* Created by JackChen on 2018/1/31.
* Version 1.0
* Description:
*/
public class StackManager {
/**
* Stack �ж�Ӧ��Activity�б� ��Ҳ����д�� Stack<Activity>��
*/
private static Stack mActivityStack;
private static StackManager mInstance;
/**
* @���� ��ȡջ������
* @return ActivityManager
*/
public static StackManager getStackManager() {
if (mInstance == null) {
mInstance = new StackManager();
}
return mInstance;
}
/**
* �Ƴ�ջ��Activity
*/
public void popActivity(Activity activity) {
if (activity != null) {
activity.finish();
mActivityStack.remove(activity);
activity = null;
}
}
/**
* ��õ�ǰջ��Activity
*/
public Activity currentActivity() {
//lastElement()��ȡ������Ԫ�أ�������ջ����Activity
if(mActivityStack == null || mActivityStack.size() ==0){
return null;
}
Activity activity = (Activity) mActivityStack.lastElement();
return activity;
}
/**
* ����ǰActivity����ջ��
*/
public void pushActivity(Activity activity) {
if (mActivityStack == null) {
mActivityStack = new Stack();
}
mActivityStack.add(activity);
}
/**
* ����ָ����clsss����ջ������������Activity
* @clsss : ָ������
*/
public void popTopActivitys(Class clsss) {
while (true) {
Activity activity = currentActivity();
if (activity == null) {
break;
}
if (activity.getClass().equals(clsss)) {
break;
}
popActivity(activity);
}
}
/**
* ����ջ������Activity
*/
public void popAllActivitys() {
while (true) {
Activity activity = currentActivity();
if (activity == null) {
break;
}
popActivity(activity);
}
}
}
FirstStep.this代码如下:
public class FirstStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView() ;
mStackManager.getStackManager().pushActivity(this); //把Activity添加到栈中
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstStep.this , SecondStep.class));
}
});
}
}
SecondStep.this代码如下:
public class SecondStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_step);
initView() ;
mStackManager.getStackManager().pushActivity(this); //把Activity添加到栈中
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondStep.this , ThirdStep.class));
}
});
}
}
ThirdStep.this代码如下:
public class ThirdStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_step);
initView() ;
mStackManager.getStackManager().pushActivity(this); //把Activity添加到栈中
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ThirdStep.this , FourthStep.class));
}
});
}
}
FourthStep.this代码如下:
public class FourthStep extends BaseActivity {
private Button btn_finish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth_step);
initView() ;
mStackManager.getStackManager().pushActivity(this); //把Activity添加到栈中
}
private void initView() {
btn_finish = (Button) findViewById(R.id.btn_finish);
btn_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mStackManager.getStackManager().popAllActivitys();
}
});
}
}
3.2>:使用list集合:
在BaseApplication中定义list集合,然后定义addActivity()<用于在每个Activity初始化时将该Activity添加到集合中>、clearActivity()<用于清空list集合>、exit()方法<用于遍历集合退出所有Activity>,然后在每个Activity的onCreate()方法中调用addActivity(),在onDestroy()中调用clearActivity(),然后在最后一个Activity调用exit()即可,具体代码如下:
/**
* Email 2185134304@qq.com
* Created by JackChen on 2018/1/31.
* Version 1.0
* Description:
*/
public class AppApplication extends Application {
private static AppApplication mAppApplication;
/** ��ǰ��activity�б� */
public ArrayList<Activity> activityList;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mAppApplication = this;
}
/** ��ȡApplication */
public static AppApplication getApp() {
if (mAppApplication == null) {
mAppApplication = new AppApplication();
}
return mAppApplication;
}
/** 给集合添加activity */
public void addActivity(Activity acitivity) {
if(activityList == null){
activityList = new ArrayList<Activity>();
}
activityList.add(acitivity);
}
/** 从集合移除activity */
public void clearActivity(){
activityList.clear();
}
/** 遍历集合,清除集合所有的activity */
public void exit() {
for (Activity activity : activityList) {
activity.finish();
}
clearActivity();//ǧ��ǵ����ȡ�����á�
System.exit(0);
}
}
FirstStep.this代码如下:
public class FirstStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView() ;
AppApplication.getApp().addActivity(this); //把Activity添加到集合中
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstStep.this , SecondStep.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
AppApplication.getApp().clearActivity(); //清除list集合
}
}
SecondStep.this代码如下:
public class SecondStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_step);
initView() ;
AppApplication.getApp().addActivity(this); //把Activity添加到集合中
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondStep.this , ThirdStep.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
AppApplication.getApp().clearActivity(); //清除list集合
}
}
ThirdStep.this代码如下:
public class ThirdStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_step);
initView() ;
AppApplication.getApp().addActivity(this); //把Activity添加到集合中
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ThirdStep.this , FourthStep.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
AppApplication.getApp().clearActivity(); //清除list集合
}
}
FourthStep.this代码如下:
public class FourthStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth_step);
initView() ;
AppApplication.getApp().addActivity(this); //把Activity添加到集合中
}
private void initView() {
Button btn_finish = (Button) findViewById(R.id.btn_finish);
btn_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppApplication.getApp().exit();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
AppApplication.getApp().clearActivity(); //清除list集合
}
}
3.3>:使用广播
在BaseActivity中,注册、接收广播,然后让每个Activity继承BaseActivity,并且在每个Activity的onDestroy()中注销广播,在最后一个Activity发送广播,然后在BaseActivity中接收广播的onReceive()方法中finish()即可,具体代码如下:
/**
* Email 2185134304@qq.com
* Created by JackChen on 2018/1/31.
* Version 1.0
* Description:
*/
public class BaseActivity extends Activity {
//用于广播的标志
public final static String INIENT_FINISH = "com.allfinish.method.finish";
//用于startActivityForResult()的标志
public final static int FINISH_REQUESTCODE = 1;
public final static int FINISH_RESULTCODE = 1;
public StackManager mStackManager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* 注册广播
*/
public void initReceiver(){
IntentFilter filter = new IntentFilter() ;
filter.addAction(INIENT_FINISH);
registerReceiver(mRceiver , filter) ;
}
/**
* 初始化广播接收者
*/
public BroadcastReceiver mRceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (INIENT_FINISH.equals(intent.getAction())){
finish();
}
}
} ;
/**
* 用于startActivityForResult()
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FINISH_REQUESTCODE){
if (resultCode == FINISH_RESULTCODE){
setResult(FINISH_REQUESTCODE);
finish();
}
}
}
}
FirstStep.this代码如下:
public class FirstStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView();
initReceiver();
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstStep.this , SecondStep.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mRceiver);
}
}
SecondStep.this代码如下:
public class SecondStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_step);
initView();
initReceiver();
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondStep.this , ThirdStep.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mRceiver);
}
}
ThirdStep.this代码如下:
public class ThirdStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_step);
initView();
initReceiver();
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ThirdStep.this , FourthStep.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mRceiver);
}
}
FourthStep.this代码如下:
public class FourthStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth_step);
initView();
initReceiver();
}
private void initView() {
Button btn_finish = (Button) findViewById(R.id.btn_finish);
btn_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendFinish();
}
});
}
private void sendFinish() {
getApplicationContext().sendBroadcast(new Intent(INIENT_FINISH));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mRceiver);
}
}
3.4>:使用startActivityForResult()方法跳转,然后用请求吗和结果码判断:
在第一、第二、第三个Activity中使用startActivityForResult(new Intent(FirstStep.this , SecondStep.class) , FINISH_REQUESTCODE)携带请求码跳转Activity,然后在最后一个Activity使用setResult(FINISH_RESULTCODE);用结果码,然后finish()最后一个Activity,然后在BaseActivity中写onActivityResult()方法,使用返回码和结果码,如果自己定义的请求码和返回码与返回的相等,就执行setResult(FINISH_RESULTCODE);并且finish()即可,具体代码如下:
FirstStep.this代码如下:
public class FirstStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView() ;
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(FirstStep.this , SecondStep.class) , FINISH_REQUESTCODE);
}
});
}
}
SecondStep.this代码如下:
public class SecondStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_step);
initView() ;
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(SecondStep.this , ThirdStep.class) , FINISH_REQUESTCODE);
}
});
}
}
ThirdStep.this代码如下:
public class ThirdStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_step);
initView() ;
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(ThirdStep.this , FourthStep.class) , FINISH_REQUESTCODE);
}
});
}
}
FourthStep.this代码如下:
public class FourthStep extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth_step);
initView() ;
}
private void initView() {
Button btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(FINISH_RESULTCODE); //结果码
finish();
}
});
}
}
3.5>:使用 FLAG_ACTIVITY_CLEAR_TOP 退出整个程序,不过不常用:
从第一个Activity跳转到第二个Activity,第二个Activity跳转到第三个Activity,第三个Activity跳转到第四个Activity,然后从第四个跳转到第一个Activity需要使用 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
代码如下:
FirstStep.this代码如下:
public class FirstStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView() ;
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstStep.this , SecondStep.class));
}
});
}
}
SecondStep.this代码如下:
public class SecondStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView() ;
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondStep.this , ThirdStep.class));
}
});
}
}
ThirdStep.this代码如下:
public class ThirdStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_step);
initView() ;
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ThirdStep.this , FourthStep.class));
}
});
}
}
FourthStep.this代码如下:
public class FourthStep extends BaseActivity {
private Button btn_next_step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth_step);
initView() ;
}
private void initView() {
btn_next_step = (Button) findViewById(R.id.btn_next_step);
btn_next_step.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FourthStep.this , MainActivity.class) ;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
startActivity(intent);
}
});
}
}
ok,到这里,我们就全部讲完了退出多个Activity的5种方法,大家有什么不懂的,不太明白的可以直接看注释,代码中注释很详细,如果还是不太懂的可以加我QQ:2185134304.
具体代码已上传至github:
https://github.com/shuai999/FinishMoreActivity.git