作者:略 | 来源:互联网 | 2023-09-04 15:42
当有3个activity时,在第三个activity想退出程序,那么就要按back键3次才能退出,那么怎么直接退出呢。
1、创建一个专门的集合类来管理所有活动
package com.yuanlp.taskstack;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 原立鹏 on 2017/6/27.
*/
public class ActivityCollector {
public static List activityList=new ArrayList();
public static void addActivity(Activity activity){
activityList.add(activity);
}
public static void removeActivity(Activity activity){
activityList.remove(activity);
}
public static void finshiAll(){
for (Activity activity: activityList){
if (!activity.isFinishing()){ //判断当前activity是否被干掉了
activity.finish();
}
}
activityList.clear();
}
}
2、创建一个java的类,BaseActivity,继承AppCompatActivity。为什么不直接写成activity呢,因为我们不需要讲BaseActivity在Manifext.xml里注册。
package com.yuanlp.taskstack;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
/**
* Created by 原立鹏 on 2017/6/27.
*/
public class BaseActivity extends AppCompatActivity{
private static final String TAG = "BaseActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, getClass().getSimpleName()); //获取当前活动的名称
ActivityCollector.addActivity(this); //调用自己写的activity的集合类,把当前activity加入到list中
}
protected void onDestroy(){
super.onDestroy();
ActivityCollector.removeActivity(this); //每当当前activity销毁时,去除当前activity
}
}
3、让3个activity都继承BaseActivity,这样之前写的三个activity也不会报错。
package com.yuanlp.taskstack;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends BaseActivity {
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Task id is: "+getTaskId()); //输出当前返回栈的ID
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
public void open1(View view){
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
public void open2(View view){
Intent intent =new Intent(this,SecondActivity.class);
startActivity(intent);
}
}
4、在第三个activity中加入一个按钮,点击后退出程序。
package com.yuanlp.taskstack;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class ThirdActivity extends BaseActivity {
private static final String TAG = "ThirdActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Task id is"+getTaskId());
setContentView(R.layout.activity_third);
}
public void exitApp(View view){
ActivityCollector.finshiAll(); //退出
android.os.Process.killProcess(android.os.Process.myPid()); //杀死当前进程
}
}