activity管理类 appManager

程序启动第一个界面类: net.oschina.app.AppStart
功能描述:一张图片
代码细节描述:一个透明度的动画效果,效果动画完成后自动启动新的Activity(Main)

 

基本BaseActivity类,继承自Activity
功能描述:分别在onCreate方法中添加Activity到栈 ; 在onDestroy方法中移除Activity
代码细节描述: 提供了一个单例类AppManager,把创建的Activity放入到栈中进行管理,提供添加Activity , 移除当前Activity , 移除指定的Activity , 移除所有的Activity ,获得当前的Activity , 退出程序。 这个工具类非常适合与用作开发使用,具体代码如下:

复制代码
import java.util.Stack;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

/**
 * 应用程序Activity管理类:用于Activity管理和应用程序退出
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class AppManager {
    
    private static Stack<Activity> activityStack;
    private static AppManager instance;
    
    private AppManager(){}
    /**
     * 单一实例
     */
    public static AppManager getAppManager(){
        if(instance==null){
            instance=new AppManager();
        }
        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){
            activityStack.remove(activity);
            activity.finish();
            activity=null;
        }
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls){
        for (Activity activity : activityStack) {
            if(activity.getClass().equals(cls) ){
                finishActivity(activity);
            }
        }
    }
    /**
     * 结束所有Activity
     */
    public void finishAllActivity(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (null != activityStack.get(i)){
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }
    /**
     * 退出应用程序
     */
    @SuppressWarnings("deprecation")
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {    }
    }
}
复制代码
 

仿Activity管理写的Fragment管理
复制代码
/**
 * @author lee0000
 * @version 1
 * @time 10月7日
 * */

public class FraManager {
    private static Stack<Fragment> fragmentStack;
    private static FraManager instance;
    private static final Object obj = new Object();

    private FraManager() {
    }

    /**
     * 单一实例
     */
    public static FraManager getFraManager() {
        if (instance == null) {
            synchronized (obj) {
                if (instance == null) {
                    instance = new FraManager();
                }
            }
        }
        return instance;
    }
    
    /**
     * 添加Fragment到堆栈
     */
    public static void pushFragment(FragmentActivity fact, Fragment fragment, int layoutID, String... title){
        if(fragmentStack==null){
            fragmentStack=new Stack<Fragment>();
        }
        fragmentStack.push(fragment);
        FragmentTransaction ft = fact.getSupportFragmentManager().beginTransaction();
        
        ft.replace(layoutID, fragment);
        if (fragmentStack.size() > 1) {
            ft.addToBackStack(null);
        }
        ft.commit();
    }
    
    /**
     * 弹出Fragment
     */
    public static Fragment popFragment(FragmentActivity fact) {
        Fragment fragment = fragmentStack.pop();
        FragmentTransaction ft = fact.getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
        return fragment;
    }
    
    /***
     * 弹出指定Activity中所有的fragment
     * */
    public static void finishFragment(FragmentActivity fact){
        for (int i = 0, size = fragmentStack.size(); i < size; i++){
            Fragment stackFragment = fragmentStack.get(i);
            if (null != stackFragment){
                Fragment fragment = stackFragment;
                if (fact == fragment.getActivity()) {
                    FragmentTransaction ft = fragment.getFragmentManager().beginTransaction();
                    ft.remove(fragment);
                    fragmentStack.remove(i);
                    ft.commit();
                }
            }
        }
    }
    
    /**
     * 清除栈中所有的项
     * */
    public static void clearStack(){
        if (fragmentStack != null && fragmentStack.isEmpty()) {
            fragmentStack.clear();
        }
    }
}
复制代码

 

posted @ 2015-05-18 19:58  wikiki  阅读(370)  评论(0编辑  收藏  举报