核心代码:
实现代码:自己写一个ActivitManager
public class ActivityManager {
private ArrayList<Activity> mActivityArray = new ArrayList<Activity>();
private Map<String, Integer> mActivityMap = new Hashtable<String, Integer>();
public static void addActivity(ActivityManager actManager,String name, Activity act)
{
actManager.addActivity(name, act);
}
public void addActivity(String name, Activity act)
{
int nCount = mActivityArray.size();
mActivityArray.add(act);
mActivityMap.put(name, nCount);
}
public static void removeActivity(ActivityManager actManager,String name, Activity act)
{
actManager.removeActivity(name, act);
}
public void removeActivity(String name, Activity act) {
Integer nIndex = mActivityMap.get(name);
if (null == nIndex)
return;
int nCount = mActivityArray.size();
if (nIndex >= nCount) {
for (int i = nCount - 1; i >= 0; i--) {
Activity activity = mActivityArray.get(i);
if (activity == act) {
mActivityMap.remove(name);
mActivityArray.remove(act);
break;
}
}
} else {
if (nIndex < nCount) {
if (act == mActivityArray.get(nIndex)) {
mActivityArray.remove(nIndex);
mActivityMap.remove(name);
} else {
mActivityArray.remove(act);
}
} else {
mActivityArray.remove(act);
}
}
}
public static void killMyProcess(ActivityManager actManager)
{
actManager.killMyProcess();
}
public void killMyProcess()
{
int nCount = mActivityArray.size();
for (int i = nCount - 1; i >= 0; i--) {
Activity activity = mActivityArray.get(i);
activity.finish();
}
mActivityArray.clear();
mActivityMap.clear();
android.os.Process.killProcess(android.os.Process.myPid());
}
public void gotoHome1InquireActivity()
{
int nCount = mActivityArray.size();
for (int i = nCount - 1; i >= 0; i--) {
Activity activity = mActivityArray.get(i);
if(! ( activity instanceof Home1InquireActivity ) ){
activity.finish();
mActivityArray.remove(i);
if(mActivityMap.containsValue(i)){
for (Map.Entry<String, Integer> entry : mActivityMap.entrySet()) {
if(entry.getValue() == i){
mActivityMap.remove(entry.getKey());
break;
}
}
}
}else{
break;
}
}
}
public void gotoHome2InquireActivity()
{
int nCount = mActivityArray.size();
for (int i = nCount - 1; i >= 0; i--) {
Activity activity = mActivityArray.get(i);
if(! ( activity instanceof Home2InquireActivity ) ){
activity.finish();
mActivityArray.remove(i);
if(mActivityMap.containsValue(i)){
for (Map.Entry<String, Integer> entry : mActivityMap.entrySet()) {
if(entry.getValue() == i){
mActivityMap.remove(entry.getKey());
break;
}
}
}
}else{
break;
}
}
}
}
如果进入一个Activity 就finish()上一个Activity,只要home那一页向新的Activity调转时,不要finish()homeActivity,android栈就会保存着那个home Activity, 所以在后面任意一个activity 不要startActivity(),直接finish(),就可以直接回到homeActivity.
另外一种高级点的方法就是上面讲的,相当于维护一个队列,拿着所有Activity的指引,删除所有的。。。