一、简述
当项目中的Activity多了就会涉及到管理的问题,比如:想看下当前已经启动了多少了Activity,可以在随意位置关闭所有的Activity等。
如果想实现这个功能,可以自定义一个类用于管理所有的启动的Activity和销毁的Activity。同时需要自定义一个BaseActivity,在这个类中增加启动的Activity和移除Activity等功能,这样当自定义一个Activity时只需要继承自这个BaseActivity就可以了。
下面将使用一个demo演示的方式来说明如何实现这个功能和如何使用,同时针对大部分功能给出测试。
二、demo演示
1)自定义一个类用于管理所有的Activity
这个类中实现了以下几个方法:
增加Activity
移除Activity
结束所有的Activity
检测某个Activity是否已启动
获取当前已存在的Activity的个数
杀掉当前的进程
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by wangwentao on 2017/2/21.
* 用于管理Activity
*/
public class ActivityManage {
public static List activityList = new ArrayList<>();
//增加Activity
public static void addActivity(Activity activity){
activityList.add(activity);
}
//移除Activity
public static void removeActivity(Activity activity){
activityList.remove(activity);
}
//结束所有的Activity
public static void finishAll(){
for(Activity activity : activityList){
if(!activity.isFinishing()){
activity.finish();
}
}
activityList.clear();
}
/**
* 判断是否存在指定的Activity
* 返回值:true,表示存在;false表示不存在
*/
public static boolean containsActivity(Activity activity){
if(activityList != null){
return activityList.contains(activity);
}else{
return false;
}
}
//获取当前Activity的个数
public static int getActivitiesCount(){
if(activityList != null){
return activityList.size();
}else{
return 0;
}
}
//杀掉当前进程(在有些情况下,当所有的Activity都销毁的情况可以考虑杀掉当前进程)
public static void killProcess(){
android.os.Process.killProcess(android.os.Process.myPid());//杀掉当前进程
}
}
以上代码已给出了详细的注释。
2)自定义一个BaseActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.mobile.cdtx.blog.main.tools.ActivityManage;
/**
* Created by wangwentao on 2017/2/21.
* 供其他Activity继承使用的Activity,可以在这里做些公共的操作
* 1)增加了管理Activity
* 2)增加了生命周期的打印
* 3)增加了打印包名和类名-主要目的是为了分析问题
*/
public class BaseActivity extends AppCompatActivity {
private static final String TAG_BASE = "BaseActivity->";
//输出日志标志
protected final String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
ActivityManage.addActivity(this);
Log.d(TAG_BASE+TAG, "onCreate: ");
printLogWithName();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG_BASE+TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG_BASE+TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG_BASE+TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG_BASE+TAG, "onStop: ");
}
@Override
protected void onDestroy(){
super.onDestroy();
ActivityManage.removeActivity(this);
Log.d(TAG_BASE+TAG, "onCreate: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG_BASE+TAG, "onRestart: ");
}
//打印包名及类名
private void printLogWithName(){
Log.d(TAG_BASE+TAG, "printLogWithName: "+getLocalClassName());
Log.d(TAG_BASE+TAG, "printLogWithName: "+getPackageName());
Log.d(TAG_BASE+TAG, "printLogWithName: "+getClass().getName());
Log.d(TAG_BASE+TAG, "printLogWithName: "+getClass().getSimpleName());
Log.d(TAG_BASE+TAG, "printLogWithName: "+getClass().getCanonicalName());
Log.d(TAG_BASE+TAG, "printLogWithName: Activity任务栈ID:"+this.getTaskId());
}
}
3)自定义一个Activity继承自BaseActivity
布局文件activity_manage_test.xml
效果图如下:
继承自BaseActivity的代码:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.mobile.cdtx.blog.R;
import com.mobile.cdtx.blog.main.base.BaseActivity;
import com.mobile.cdtx.blog.main.tools.ActivityManage;
public class ActivityManageTestActivity extends BaseActivity implements View.OnClickListener{
private static final String TAG = "ActivityManageTestActiv";
Button btnTwo,btnThree,btnFour,btnFive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
setContentView(R.layout.activity_manage_test);
initView();//初始化控件
}
//初始化控件
private void initView(){
btnTwo = (Button)findViewById(R.id.id_btn_checkActivity);
btnTwo.setOnClickListener(this);
btnThree = (Button)findViewById(R.id.id_btn_getCount);
btnThree.setOnClickListener(this);
btnFour = (Button)findViewById(R.id.id_btn_finishActivity);
btnFour.setOnClickListener(this);
btnFive = (Button)findViewById(R.id.id_btn_finish);
btnFive.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.id_btn_checkActivity:
checkActivityExist();
break;
case R.id.id_btn_getCount:
getActivityCount();
break;
case R.id.id_btn_finishActivity:
cleanActivity();
break;
case R.id.id_btn_finish:
finish();
break;
}
}
//检测Activity是否存在
private void checkActivityExist(){
Log.d(TAG, "checkActivityExist: "+ ActivityManage.containsActivity(this));
}
//获取Activity的个数
private void getActivityCount(){
Log.d(TAG, "checkActivityExist: "+ ActivityManage.getActivitiesCount());
}
//清除所有的Activity
private void cleanActivity(){
ActivityManage.finishAll();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
}
4)测试代码
代码中从5个角度进行了测试:
1、查看生命周期和当前Activity的任务栈等信息
界面启动后打印的日志如下:
02-22 11:23:15.542 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onCreate:
02-22 11:23:15.542 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: printLogWithName: main.activity.ActivityManageTestActivity
02-22 11:23:15.542 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: printLogWithName: com.mobile.cdtx.blog
02-22 11:23:15.542 23469-23469/com.mobile.cdtx.blog V/Activity: onCreate com.mobile.cdtx.blog.main.activity.ActivityManageTestActivity@421f7d98: null
02-22 11:23:15.552 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: printLogWithName: com.mobile.cdtx.blog.main.activity.ActivityManageTestActivity
02-22 11:23:15.552 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: printLogWithName: ActivityManageTestActivity
02-22 11:23:15.552 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: printLogWithName: com.mobile.cdtx.blog.main.activity.ActivityManageTestActivity
02-22 11:23:15.552 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: printLogWithName: Activity任务栈ID:462
02-22 11:23:15.552 23469-23469/com.mobile.cdtx.blog D/ActivityManageTestActiv: onCreate:
2、检测Activty是否存在
02-22 11:26:05.282 23469-23469/com.mobile.cdtx.blog D/ActivityManageTestActiv: checkActivityExist: true
将当前的Activity去查询是否存在Activity管理链表中,结果显示存在
3、获取Activity的个数
02-22 11:27:22.582 23469-23469/com.mobile.cdtx.blog D/ActivityManageTestActiv: checkActivityExist: 1
这里只演示了一个Activity继承自BaseActivity,所以打印的个数为1
4、结束所有的Activity
02-22 11:29:38.692 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onPause:
02-22 11:29:39.082 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onStop:
02-22 11:29:39.092 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onCreate:
02-22 11:29:39.092 23469-23469/com.mobile.cdtx.blog I/ActivityManageTestActiv: onDestroy:
看到的现象是界面销毁了,因为Activity管理中在清除Activity之前做了该Activity的finish()操作,所以界面会自动消失
5、调用退出
02-22 11:39:51.842 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onPause:
02-22 11:39:52.262 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onStop:
02-22 11:39:52.262 23469-23469/com.mobile.cdtx.blog D/BaseActivity->ActivityManageTestActivity: onCreate:
02-22 11:39:52.262 23469-23469/com.mobile.cdtx.blog I/ActivityManageTestActiv: onDestroy:
注意:这时由于是自己在Activity中主动调用了finish();链表管理中的添加的Activity并没有清除。
最后不要忘记,所有使用的Activity要在AndroidManifest.xml中注册。