ActivityGroup自我堆栈管理(复用现有activity)

阅读更多

密码管理请下载:

http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword

 

package com.dczs.activity;

import java.util.Stack;
import android.app.Activity;
import android.app.ActivityGroup;
import android.util.Log;
import android.view.KeyEvent;

public class BackStackActivityGroup extends ActivityGroup {

	private static final String TAG = "BackStackActivityGroup";

	private AcitivityStack mActivityStack = new AcitivityStack();

	public interface IActivityStack {

		void add(ActivityStatus name);

		void remove(String name);

		void pop();
	}

	public void onStackChange(AcitivityStack stack) {

	}

	public class AcitivityStack implements IActivityStack {

		public Stack mStack = new Stack();

		@Override
		public void add(ActivityStatus activityStackItem) {
			mStack.add(activityStackItem);
			onStackChange(this);
		}

		@Override
		public void remove(String name) {

			ActivityStatus theItem = null;

			for (ActivityStatus item : mStack) {
				if (item.name == name) {
					theItem = item;
					break;
				}
			}
			if (theItem != null) {
				mStack.remove(theItem);
				if (mStack.size() > 0) {
					mStack.peek().run();
				} else {
					getApplication().onTerminate();
				}
			}
			onStackChange(this);
		}

		@Override
		public void pop() {
			mStack.pop();
			if (mStack.size() > 0) {
				mStack.peek().run();
			} else {
				getApplication().onTerminate();
			}
			onStackChange(this);
		}

		public int size() {
			return mStack.size();
		}

		public String peek() {
			if (mStack.size() > 0 && mStack.peek() != null) {
				return mStack.peek().name;
			} else {
				return null;
			}
		}

	}

	public class ActivityStatus {

		private IActivityStack mActivityStack;

		private String name = "";

		private boolean mAddToStack = false;

		private Runnable runnable;

		private Activity activity;

		private ActivityStatus(IActivityStack activityStack) {
			mActivityStack = activityStack;
		}

		public void setRunnable(Runnable runnable) {
			this.runnable = runnable;
		}

		public void setName(String name) {
			this.name = name;
		}

		public ActivityStatus() {
		}

		public void addToBackStack() {
			mAddToStack = true;
		}

		public void run() {
			runnable.run();
			activity = getLocalActivityManager().getActivity(name);
		}

		public void commit() {
			if (mAddToStack) {
				mActivityStack.add(this);
			}
			runnable.run();
			activity = getLocalActivityManager().getActivity(name);
		}
	};

	public ActivityStatus startTransaction() {
		return new ActivityStatus(mActivityStack);
	}

	public AcitivityStack getActivityStack() {
		return mActivityStack;
	}

	/**
	 * Back event should not directly dispatch to the child views. Because when
	 * back key event dispatch to this activity's child views, if any of a child
	 * activity's sub views is taking focus. the child activity's
	 * {@link DecorView} will receive the event, and dispatch back key event to
	 * the child activity and call the child activity's onBackPressed method,
	 * this activity's onBackPressed will not be called. This activity's
	 * onBackPressed event will depend on the child activity's views' focus
	 * change.
	 * 
	 * @see {@link #onGlobalFocusChanged}.
	 */
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		Log.d(TAG, "dispatchKeyEvent... new ");
		if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
			super.dispatchKeyEvent(event);
		} else {
			if (event.getAction() == KeyEvent.ACTION_UP) {
				onBackPressed();
			}
		}
		return true;
	}

	@Override
	public void onBackPressed() {
		Log.d(TAG, "onBackPressed... new ");
		Activity activity = getLocalActivityManager().getActivity(
				mActivityStack.peek());
		activity.onBackPressed();
	}

	@Override
	public void finishFromChild(Activity child) {
		// TODO Auto-generated method stub
		Log.d(TAG, "finishFromChild... new ");
		for (ActivityStatus item : mActivityStack.mStack) {
			Activity activity = getLocalActivityManager()
					.getActivity(item.name);
			if (activity == child) {
				// handled the finish command.
				Log.d(TAG, "finishFromChild... is Finishing is true");
				AcitivityStack stack = getActivityStack();
				stack.remove(item.name);
			}
		}
	}
}

 

你可能感兴趣的:(ActivityGroup自我堆栈管理(复用现有activity))