需求:某层次很深的一个acticity有个退出登录按钮
难点:for循环finish太不优雅了
实现:
第一步: 工具类
import android.app.Activity;
import java.util.Stack;
/**
* Created by jing on 17/5/24.
* 管理 Activity 的 视图栈
*/
public class KapApplicationActivitysQueue {
private KapApplicationActivitysQueue(){}
private static KapApplicationActivitysQueue queue = new KapApplicationActivitysQueue();
public static KapApplicationActivitysQueue ShareActivityQueue(){
return queue;
}
private Stack<Activity> activityStack = new Stack<Activity>();
/** 获取当前的activity,不做任何操作 */
public Activity currentActivity() {
return activityStack.lastElement();
}
/**
* 只有这俩方法 操作 activityStack不能手动调用(都是自动添加删除的)
* addActivity 添加Activity到堆栈
* popCurrentActivity 结束当前Activity
* currentActivity
*/
public void addActivity(Activity activity) {
activityStack.push(activity);
}
public void popCurrentActivity(Activity activity) {
activityStack.remove(activity);
}
/**
* 下面的这些方法都是辅助方法 (注意防止当前activity结束当前的导致crash)
* 获取当前Activity(堆栈中最后一个压入的)
*/
// 结束指定的Activity
public void finishOneActivity(Activity activity) {
if (activity != null) {
if(!activity.isFinishing()) {
activity.finish();
}
}
}
// 结束指定类名的Activity
public void finishOneActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (!activity.getClass().equals(cls)) continue;
finishOneActivity(activity);
return;
}
}
/** 结束至指定类名Activity(不包括该类名) */
public void finishToActiovity(Class<?> cls){
while (!activityStack.lastElement().getClass().equals(cls)){
activityStack.pop().finish();
if (activityStack.size() == 0) return;
}
}
/** 结束除指定类名的所有Activity */
public void finishExcludeActivityAllActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity == null) continue;
if (activity.getClass().equals(cls)) continue;
finishOneActivity(activity);
}
}
/** 结束所有Activity */
public void finishAllActivity() {
for (Activity activity : activityStack) {
if (activity == null) continue;
finishOneActivity(activity);
}
}
}
第二步:自定义的Application里面
public class KapApplication extends Application{
/**
* activity栈管理
* */
private KapApplicationActivitysQueue activitysQueue = KapApplicationActivitysQueue.ShareActivityQueue();
private ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
activitysQueue.addActivity(activity);//创建
}
@Override
public void onActivityDestroyed(Activity activity) {
activitysQueue.popCurrentActivity(activity);// 推出
}
@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 bundle) {
}
};
// 下面是辅助方法,仅供参考
/** 会退至至指定的activity(此class成为当前显示的) */
public static void popToActivity(Class cls){
KapApplicationActivitysQueue activitysQueue = getInstance().activitysQueue;
activitysQueue.finishToActiovity(cls);
}
/** 获取当前activity */
public static Activity currentActivity(){
KapApplicationActivitysQueue activitysQueue = getInstance().activitysQueue;
return activitysQueue.currentActivity();
}
/** 切换根视图登录 */
public static void goLoginActivity(){
Class openClass = KapLoginActivity.class;
changeRootActivityByClass(openClass);
}
/** 切换根视图主页 */
public static void goMainActivity(){
Class openClass = KapMainActivity.class;
changeRootActivityByClass(openClass);
}
private static void changeRootActivityByClass(Class openClass){
KapApplicationActivitysQueue activitysQueue = getInstance().activitysQueue;
Activity currentActivity = activitysQueue.currentActivity();
currentActivity.startActivity(new Intent(currentActivity, openClass));
activitysQueue.finishExcludeActivityAllActivity(openClass);
}
}
使用:
private void pushActivity(){
KapApplication.goLoginActivity();//登录页
KapApplication.goMainActivity();//主页
KapApplication.popToActivity(KapMainActivity.class);// 回退到主页
}