android的Activity管理


接触android开发,不得不要考虑Activity的生命周期,对activity的管理也至关重要,不多说,贴代码:

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;

/**
 * activity管理
 * @author Liujy
 *
 */
public class KYActivityMannger extends Application {
	private List<Activity> mList = new LinkedList<Activity>();
	private List<Activity> mLogoutList = new LinkedList<Activity>();
	private static KYActivityMannger instance;

	private KYActivityMannger() {
	}

	public synchronized static KYActivityMannger getInstance() {
		if (null == instance) {
			instance = new KYActivityMannger();
		}
		return instance;
	}

	/**
	 *  add Activity save mList
	 * @param activity
	 */
	public void addActivity(Activity activity) {
		mList.add(activity);
	}

	/**
	 *  add Activity save mLogoutList
	 * @param activity
	 */
	public void addLogoutActivity(Activity activity) {
		mLogoutList.add(activity);
	}

	/**
	 * 退出
	 */
	public void exit() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			android.os.Process.killProcess(android.os.Process.myPid());
			System.exit(0);
		}
	}

	/**
	 * 注销
	 */
	public void logout() {

		try {
			for (Activity activity : mLogoutList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.gc();
		}
	}

	public void onLowMemory() {
		super.onLowMemory();
		System.gc();
	}
}


在每个activity中添加以下代码:

KYActivityMannger.getInstance().addActivity(this);
KYActivityMannger.getInstance().addLogoutActivity(this);

转载请注明出处:http://blog.csdn.net/elsdnwn/article/details/25197667


你可能感兴趣的:(android,Android开发,管理,Activity)