最近打算自己把所学的Android相关内容,做成一个demo,顺便总结回顾,纯属个人记录
项目开发第一步搭建框架,当然这个根据项目大小复杂度,具体需求而定。但肯定少不了对Activity的管理,传统做法是在BaseActivity中维护一个List或者stack,这样基本满足需求。但好的程序得要一劳永逸,走走极端。万一Activity不继承BaseActivity呢,那就得在生命周期中多写一系列跟维护list相关的代码,逻辑复杂,代码也难维护。
自Android4.0(API-14)开始,Application中多了一个方法,可以全局监听Activity的生命周期(
registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callbacks)
)那简单了,我的维护List或者Stack并不需要跟BaseActivity有任何的关系了,不管你创建的是什么Activity,在Application中都是能够获取到监听回调
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
ActivityLifecycleCallbacks 基本对应Activity的生命周期,这里就不详细说了。
既然有了方法,接下来就直接开干了
首先,管理类还是需要有的
import android.app.Activity;
import android.util.Log;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* activity 管理
*/
public class AppManager {
private static final String TAG = "AppManager";
private static List list;
private static final AppManager stack = new AppManager();
public static AppManager getInstance() {
return stack;
}
private AppManager() {
list = Collections.synchronizedList(new LinkedList());
}
/**
* 添加 activity
*
* @param activity
*/
public void pushActivity(Activity activity) {
if (null != list) {
Log.e(TAG, "pushActivity: ");
list.add(activity);
}
}
/**
* 删除 activity
*
* @param activity
*/
public void popActivity(Activity activity) {
if (null != list && list.contains(activity)) {
Log.e(TAG, "popActivity: ");
list.remove(activity);
}
}
/**
* 获取 当前显示activity
*
* @return
*/
public static Activity currentActivity() {
if (null == list || list.isEmpty()) {
return null;
}
return list.get(list.size() - 1);
}
/**
* 结束 当前activity
*/
public static void finishCurrentActivity() {
if (null == list || list.isEmpty()) {
return;
}
finishActivity(list.get(list.size() - 1));
}
/**
* 结束指定的Activity
*/
public static void finishActivity(Activity activity) {
if (list == null || list.isEmpty()) {
return;
}
if (activity != null) {
list.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 结束指定类名的Activity
*/
public static void finishActivity(Class> cls) {
if (list == null || list.isEmpty()) {
return;
}
for (Activity activity : list) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
}
}
}
/**
* 按照指定类名找到activity
*
* @param cls
* @return
*/
public static Activity findActivity(Class> cls) {
Activity targetActivity = null;
if (list != null) {
for (Activity activity : list) {
if (activity.getClass().equals(cls)) {
targetActivity = activity;
break;
}
}
}
return targetActivity;
}
/**
* 结束所有Activity
*/
public static void finishAllActivity() {
if (list == null) {
return;
}
for (Activity activity : list) {
activity.finish();
}
list.clear();
}
/**
* 退出应用程序
*/
public static void appExit() {
try {
finishAllActivity();
} catch (Exception e) {
e.printStackTrace();
}
}
}
代码注释写的已经很清楚了,嗯~继续
跟传统的管理方式不一样的地方在于,并不是直接在生命周期中,去添加activity到维护List中了
首先先实现ActivityLifecycleCallbacks
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
/**
* 在application全局监听 Activity生命周期
*/
public class RegisterActivityListener implements Application.ActivityLifecycleCallbacks {
private static final String TAG = "RegisterActivityListene";
private AppManager stack;
public RegisterActivityListener() {
this.stack = AppManager.getInstance();
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.e(TAG, "onActivityCreated: " + activity.getClass().getSimpleName());
stack.pushActivity(activity);//添加到List中
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.e(TAG, "onActivityDestroyed: " + activity.getClass().getSimpleName());
stack.popActivity(activity);//从List中移除
}
}
在onActivityCreated()方法,activity创建时,添加
在onActivityDestroyed()方法,activity销毁时,移除
当然,还有更多需求也可以自己添加
最后一步了,该准备的东西都已经准备好了,接下来就剩下使用了
import android.os.Build;
import com.example.mvptest.base.BaseApplication;
import com.example.mvptest.base.RegisterActivityListener;
public class MyApplication extends BaseApplication {
@Override
public void onCreate() {
super.onCreate();
//全局监听 Activity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
registerActivityLifecycleCallbacks(new RegisterActivityListener());
}
}
}
到此,结束。一个简单的activity管理就结束了,相比较另外那种BaseActivity中手动添加管理,这种方式更加省事方便
边敲代码边记录,目标完成+1,后面会趁着项目不忙继续更新