All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.MyDroidLib.util.ActivityStack Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
/*
 * Copyright 2017 jw362j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hadoopz.MyDroidLib.util;

/**
 *
 * @author jw362j
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import android.app.Activity;
import com.hadoopz.MyDroidLib.activity.IDroidBaseActivity;

/**
 * activity堆栈管理
 * 
 * @author blue
 */
public class ActivityStack
{
	private static ActivityStack mSingleInstance;
	private final Stack mActicityStack;
        public static IDroidBaseActivity sCurrentActivity = null;
	private ActivityStack()
	{
		mActicityStack = new Stack();
	}

	public static ActivityStack getInstance()
	{
		if (null == mSingleInstance)
		{
			mSingleInstance = new ActivityStack();
		}
		return mSingleInstance;
	}

	public Stack getStack()
	{
		return mActicityStack;
	}
        
        public IDroidBaseActivity getCurrentActivity(){
		return sCurrentActivity;
	}
	public void resetCurrentActivity(IDroidBaseActivity baseActivity){
		sCurrentActivity = baseActivity ;
	}

	/**
	 * 入栈
	 * 
	 * @author blue
     * @param activity
	 */
	public void addActivity(Activity activity)
	{
		mActicityStack.push(activity);
	}

	/**
	 * 出栈
	 * 
	 * @author blue
     * @param activity
	 */
	public void removeActivity(Activity activity)
	{
		mActicityStack.remove(activity);
	}

	/**
	 * 彻底退出
	 * 
	 * @author blue
	 */
	public void finishAllActivity()
	{
		Activity activity;
		while (!mActicityStack.empty())
		{
			activity = mActicityStack.pop();
			if (activity != null)
				activity.finish();
		}
	}

	/**
	 * finish指定的activity
	 * 
	 * @author blue
     * @param actCls
     * @return 
	 */
	public boolean finishActivity(Class actCls)
	{
		Activity act = findActivityByClass(actCls);
		if (null != act && !act.isFinishing())
		{
			act.finish();
			return true;
		}
		return false;
	}

	public Activity findActivityByClass(Class actCls)
	{
		Activity aActivity = null;
		Iterator itr = mActicityStack.iterator();
		while (itr.hasNext())
		{
			aActivity = itr.next();
			if (null != aActivity && aActivity.getClass().getName().equals(actCls.getName()) && !aActivity.isFinishing())
			{
				break;
			}
			aActivity = null;
		}
		return aActivity;
	}

	/**
	 * finish指定的activity之上的所有activity
	 * 
	 * @author blue
     * @param actCls
     * @param isIncludeSelf
     * @return 
	 */
	public boolean finishToActivity(Class actCls, boolean isIncludeSelf)
	{
		List buf = new ArrayList();
		int size = mActicityStack.size();
		Activity activity = null;
		for (int i = size - 1; i >= 0; i--)
		{
			activity = mActicityStack.get(i);
			if (activity.getClass().isAssignableFrom(actCls))
			{
				for (Activity a : buf)
				{
					a.finish();
				}
				return true;
			} else if (i == size - 1 && isIncludeSelf)
			{
				buf.add(activity);
			} else if (i != size - 1)
			{
				buf.add(activity);
			}
		}
		return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy