当前位置: 代码迷 >> Android >> 在Android项目中手动治理你的Activity
  详细解决方案

在Android项目中手动治理你的Activity

热度:57   发布时间:2016-04-28 03:54:07.0
在Android项目中手动管理你的Activity
我们通常会使用系统自带的ActivityManager,但是在有些情况下使用起来不是太简洁。所以会自己来维护一个栈来管理这些activtiy

代码如下:
import java.util.Stack;import android.app.Activity;import android.util.Log;public class ScreenManager{    private static final String TAG = "ScreenManager";    private static Stack<Activity> activityStack;        private static ScreenManager instance;        /**     * <单例方法>     * <功能详细描述>     * @return 该对象的实例     * @see [类、类#方法、类#成员]     */    public static ScreenManager getScreenManager1()    {        if (instance == null)        {            instance = new ScreenManager();        }        return instance;    }        /**     * <获取当前栈顶Activity>     * <功能详细描述>     * @return     * @see [类、类#方法、类#成员]     */    public Activity currentActivity()    {        if (activityStack == null || activityStack.size() == 0)        {            return null;        }        Activity activity = activityStack.lastElement();                Log.e(TAG, "get current activity:" + activity.getClass().getSimpleName());        return activity;    }        /**     * <将Activity入栈>     * <功能详细描述>     * @param activity     * @see [类、类#方法、类#成员]     */    public void pushActivity(Activity activity)    {        if (activityStack == null)        {            activityStack = new Stack<Activity>();        }        Log.e(TAG, "push stack activity:" + activity.getClass().getSimpleName());        activityStack.add(activity);    }        /**     * <退出栈顶Activity>     * <功能详细描述>     * @param activity     * @see [类、类#方法、类#成员]     */    public void popActivity(Activity activity)    {        if (activity != null)        {            activity.finish();            Log.e(TAG, "remove current activity:" + activity.getClass().getSimpleName());            activityStack.remove(activity);            activity = null;        }    }        /**     * <退出栈中所有Activity,当前的activity除外>     * <功能详细描述>     * @param cls     * @see [类、类#方法、类#成员]     */    public void popAllActivityExceptMain(Class cls)    {        while(true)        {            Activity activity = currentActivity();            if (activity == null)            {                break;            }            if (activity.getClass().equals(cls))            {                break;            }                        popActivity(activity);        }    }}



具体使用:
 @Override    protected void onCreate(Bundle savedInstanceState)    {                super.onCreate(savedInstanceState);                ActivityManager am =  (ActivityManager)getSystemService("");        am.        ScreenManager.getScreenManager().pushActivity(this);    }    @Override    protected void onDestroy()    {        super.onDestroy();        ScreenManager.getScreenManager().popActivity(this);    }    /**     * <退出系统>     * <绑定退出按钮>     * @param v     * @see [类、类#方法、类#成员]     */    public void exitSystem(View v)    {        ScreenManager.getScreenManager().popAllActivityExceptMain(getClass());                finish();    }
  相关解决方案