标签:androi pat cli ali view com for bsp ack
1》知晓当前是哪一个活动
建立一个BaseActivity 类继承AppCompatActivity 类,然后在其中输出当前活动的名称,将原来继承AppCompatActivity 类的类改为继承BaseActivity
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("NaseActivity", getClass().getSimpleName());
ActivityCollector.add(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.remove(this);
}
}
2》活动的统一销毁
新建类ActivityCollector 在其中创建一个List同于安放新创建的活动,在需要时直接调用其中的finishiall函数将所有活动一起销毁
package com.example.activitytest;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2019/1/23.
*/
public class ActivityCollector {
public static List<Activity> activities=new ArrayList<>();
public static void add(Activity activity)
{
activities.add(activity);
}
public static void remove(Activity activity)
{
activities.remove(activity);
}
public static void finishiall(){
for (Activity activity:activities){
if(!activity.isFinishing()){
activity.finish();
}
}
}
}
调用函数销毁所有的活动时可以使用 android.os.Process.killProcess(android.os.Process.myPid());来销毁程序有利于保证数据的完全销毁
public void onClick(View v)
{
ActivityCollector.finishiall();
android.os.Process.killProcess(android.os.Process.myPid());
}
标签:androi pat cli ali view com for bsp ack
原文地址:https://www.cnblogs.com/837634902why/p/10306842.html