2014-05-08 105 views
0

我有一個具有活動A,B,C,D,E的應用程序。活動A從發射器開始,B從A,C從B,D從C和E從D,如A→B→C→D→E。 現在在活動E,我想要做:如何從應用程序的堆棧活動中獲取任何活動

for(int i = 0; i < listActivity; i ++) if(activity [i] eligible) show activity。

如何瀏覽列表活動的運行以及如何從堆棧中獲取任何活動? 希望你瞭解我並支持我解決這個問題。 謝謝大家的閱讀。

+0

你不能這樣做的活動。如果您只是創建了一個活動,然後在您的活動中將活動轉換爲片段,那麼您將可以使用FragmentManager循環訪問它們。 – Dyrborg

回答

0

A,B,C,D,E。活動擴展BaseActivity

public abstract class BaseActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    AppManager.getAppManager().addActivity(this); 
    } 
} 

那麼你可以使用AppManager.getAppManager().XXXX你做點什麼。下單?

public class AppManager { 
private static Stack<Activity> activityStack; 
private static AppManager instance; 

private AppManager() { 
} 

public static AppManager getAppManager() { 
    if (instance == null) { 
     instance = new AppManager(); 
    } 
    return instance; 
} 

/** 
* AddmActivity to stack 
*/ 
public void addActivity(Activity activity) { 
    if (activityStack == null) { 
     activityStack = new Stack<Activity>(); 
    } 
    activityStack.add(activity); 
} 

/** 
* Get current Activity(stack Top Activity) 
*/ 
public Activity currentActivity() { 
    if (activityStack == null || activityStack.isEmpty()) { 
     return null; 
    } 
    Activity activity = activityStack.lastElement(); 
    return activity; 
} 

public Activity findActivity(Class<?> cls) { 
    Activity activity = null; 
    for (Activity aty : activityStack) { 
     if (aty.getClass().equals(cls)) { 
      activity = aty; 
      break; 
     } 
    } 
    return activity; 
} 

/** 
* Finish Activity(Stack Top Activity) 
*/ 
public void finishActivity() { 
    Activity activity = activityStack.lastElement(); 
    finishActivity(activity); 
} 

/** 
* Finish someone Activity 
*/ 
public void finishActivity(Activity activity) { 
    if (activity != null) { 
     if (activityStack.contains(activity)) { 
      activityStack.remove(activity); 
      activity.finish(); 
      activity = null; 
     } 
    } 
} 

/** 
* Finish someone Activity 
*/ 
public void finishActivity(Class<?> cls) { 
    for (Activity activity : activityStack) { 
     if (activity.getClass().equals(cls)) { 
      finishActivity(activity); 
     } 
    } 
} 

/** 
* Finish All Activity except cls,if cls not in stack ,then clear all 
* @param cls 
*/ 
public void finishOthersActivity(Class<?> cls) { 
    for (Activity activity : activityStack) { 
     if (!(activity.getClass().equals(cls))) { 
      finishActivity(activity); 
     } 
    } 
} 

/** 
* Finish All Activity 
*/ 
public void finishAllActivity() { 
    for (int i = 0, size = activityStack.size(); i < size; i++) { 
     if (null != activityStack.get(i)) { 
      if (activityStack.contains(activityStack.get(i))) { 
       activityStack.get(i).finish(); 
      } 
     } 
    } 
    activityStack.clear(); 
} 

/** 
* exit 
*/ 
public void AppExit(Context context) { 
    try { 
     finishAllActivity(); 
     ActivityManager activityMgr = (ActivityManager) context 
       .getSystemService(Context.ACTIVITY_SERVICE); 
     activityMgr.killBackgroundProcesses(context.getPackageName()); 
     System.exit(0); 
     android.os.Process.killProcess(android.os.Process.myPid()); 
    } catch (Exception e) { 
     System.exit(0); 
    } 
} 

}

+0

非常感謝你,我明白 –

0

創建Base Activity以保持所有started Activity的握在你的應用程序。所有其他Activity需要擴展到Base Activity

Activity啓動後,將它添加到HashMapArrayList。我寧願在我Application類來聲明這個現在

public class MyApp extends Application { 
    public static HashMap<String , CommonActivity > mBackStackActivities 
     = new HashMap<String, CommonActivity>(); 
    } 

,創建Base Activity並把Activity實例爲HashMap

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_common); 
    MyApp.mBackStackActivities.put(getComponentName().getClassName(), this); 
} 

HashMap取出Activity其被摧毀後

@Override 
protected void onDestroy(){ 
    MyApp.mBackStackActivities.remove(getComponentName().getClassName()); 
    super.onDestroy(); 
} 

最後,在需要時檢索Activity從列表中,您還可以檢查Activity已經啓動

if(MyApp.mBackStackActivities 
     .containsKey(intent.getComponent().getClassName())){ 
+0

非常感謝你,我明白你的答案 –

0

非常感謝你的答案,@立彬和@ Gavin2058,我創建你的一類類似的措施,我保存活動列表,當我想獲得一個列出的活動,我檢查:

public static boolean checkActivityExist(Class<?> activity) { 
    int size = ActivityHistory.size(); 
    for (int i = size - 1; i >= 0; i--) { 
     Activity ac = ActivityHistory.get(i); 
     if (activity.isAssignableFrom(ac.getClass())) { 
      return true; 
     } 
    } 
    return false; 
} 

如果條件屬實,我得到的活動,並啓動它:

public static void startActiByStateTab(AbstractActivity c, STATESTAB state) { 
    int size = ActivityHistory.size(); 
    for (int i = size - 1; i >= 0; i--) { 
     AbstractActivity ac = (AbstractActivity)ActivityHistory.get(i); 
     if (ac.getIsTab() == state) { 

      Intent intent = new Intent(c, ac.getClass()); 
      intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
      c.startActivity(intent); 
      break; 
     } 

    } 
} 

但我調試和看到活動我只是開始,它來到onResume但在屏幕上是透明的,沒有任何視圖顯示在屏幕上。 我想......

相關問題