Last active
November 16, 2015 07:46
-
-
Save brucetoo/417850d13f66f72231e7 to your computer and use it in GitHub Desktop.
Activity堆栈式管理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.app.Activity; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Stack; | |
| /** | |
| * activity堆栈式管理 | |
| */ | |
| public class ActivityManager { | |
| private static Stack<Activity> activityStack; | |
| private static ActivityManager instance; | |
| private ActivityManager() { | |
| } | |
| /** | |
| * 单一实例 | |
| */ | |
| public static ActivityManager getAppManager() { | |
| if (instance == null) { | |
| synchronized (ActivityManager.class) { | |
| if(instance == null) { | |
| instance = new ActivityManager(); | |
| } | |
| } | |
| } | |
| return instance; | |
| } | |
| /** | |
| * 添加Activity到堆栈 | |
| */ | |
| public void addActivity(Activity activity) { | |
| if (activityStack == null) { | |
| activityStack = new Stack<Activity>(); | |
| } | |
| activityStack.add(activity); | |
| } | |
| /** | |
| * 获取当前Activity(堆栈中最后一个压入的) | |
| */ | |
| public Activity currentActivity() { | |
| Activity activity = activityStack.lastElement(); | |
| return activity; | |
| } | |
| /** | |
| * 结束当前Activity(堆栈中最后一个压入的) | |
| */ | |
| public void finishActivity() { | |
| Activity activity = activityStack.lastElement(); | |
| finishActivity(activity); | |
| } | |
| /** | |
| * 结束指定的Activity | |
| */ | |
| public void finishActivity(Activity activity) { | |
| if (activity != null && !activity.isFinishing()) { | |
| activityStack.remove(activity); | |
| activity.finish(); | |
| activity = null; | |
| } | |
| } | |
| /** | |
| * 结束指定类名的Activity | |
| */ | |
| public void finishActivity(Class<?> cls) { | |
| for (Activity activity : activityStack) { | |
| if (activity.getClass().equals(cls)) { | |
| finishActivity(activity); | |
| break; | |
| } | |
| } | |
| } | |
| /** | |
| * 结束所有Activity | |
| */ | |
| public void finishAllActivity() { | |
| for (int i = 0, size = activityStack.size(); i < size; i++) { | |
| if (null != activityStack.get(i)) { | |
| finishActivity(activityStack.get(i)); | |
| break; | |
| } | |
| } | |
| activityStack.clear(); | |
| } | |
| /** | |
| * 通过类名-获取指定的Activity | |
| */ | |
| public static Activity getActivity(Class<?> cls) { | |
| if (activityStack != null) | |
| for (Activity activity : activityStack) { | |
| if (activity.getClass().equals(cls)) { | |
| return activity; | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * 从堆栈中移除某个activity | |
| * @param activity | |
| */ | |
| public void removeActivity(Activity activity) { | |
| activityStack.remove(activity); | |
| } | |
| /** | |
| * 清除所有activity 返回首页 | |
| */ | |
| public void backMain() { | |
| List<Activity> list = new ArrayList<Activity>(); | |
| for (Activity act : activityStack) { | |
| if (!act.getClass().equals(MainActivity.class)) { | |
| list.add(act); | |
| } | |
| } | |
| for (Activity act : list) { | |
| if (!act.isFinishing()) { | |
| act.finish(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment