转载请注明出处:http://www.jianshu.com/p/58fc6b1f6f54
本文出自Shawpoo的简书
我的博客:CSDN博客
在日常的APP开发中,为了对APP中所有的Activity进行管理,所以统一放在Stack中处理。
- 创建AppManager:
public class AppManager {
public static Stack<Activity> getActivityStack() {
return activityStack;
}
private static Stack<Activity> activityStack;
private static AppManager instance;
private AppManager() {
}
/**
* 单一实例
*/
public static AppManager getInstance() {
if (instance == null) {
instance = new AppManager();
}
return instance;
}
/**
* 添加Activity到堆栈
*/
public void addActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<>();
}
activityStack.add(activity);
}
/**
* 获取栈顶Activity(堆栈中最后一个压入的)
*/
public Activity getTopActivity() {
return activityStack.lastElement();
}
/**
* 结束栈顶Activity(堆栈中最后一个压入的)
*/
public void finishTopActivity() {
Activity activity = activityStack.lastElement();
finishActivity(activity);
}
/**
* 结束指定类名的Activity
*
* @param cls
*/
public void finishActivity(Class<?> cls) {
Iterator iterator = activityStack.iterator();
while (iterator.hasNext()) {
Activity activity = (Activity) iterator.next();
if (activity.getClass().equals(cls)) {
iterator.remove();
activity.finish();
}
}
}
/**
* 结束所有Activity
*/
@SuppressWarnings("WeakerAccess")
public void finishAllActivity() {
for (int i = 0, size = activityStack.size(); i < size; i++) {
if (null != activityStack.get(i)) {
activityStack.get(i).finish();
}
}
activityStack.clear();
}
/**
* 退出应用程序
*/
public void appExit() {
try {
finishAllActivity();
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
} catch (Exception e) {
}
}
/**
* 结束指定的Activity
*/
public void finishActivity(Activity activity) {
if (activity != null) {
activityStack.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 得到指定类名的Activity
*/
public Activity getActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
return activity;
}
}
return null;
}
}
- 使用:为了进行统一处理,我们可以写在BaseActivity中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
AppManager.getInstance().addActivity(this); //添加到栈中
}
@Override
protected void onDestroy() {
super.onDestroy();
AppManager.getInstance().finishActivity(this); //从栈中移除
}
网友评论