简体   繁体   English

如何检查活动后堆栈中的实例并删除其中一个活动

[英]How to check for a instance in back stack of activities and remove one of the activities

I have 3 activities , stacked one above another我有3 activities ,一个叠放在另一个上面

A -> B -> C -> D
  • Now When I am in D , How to find that B is available in backstack现在当我在D ,如何在后台堆栈中找到 B 可用
  • Now when B is present in back stack remove only B from the stack A,B,C,D现在,当 B 存在于后堆栈中时,仅从堆栈 A、B、C、D 中删除 B
  • Then launch B again ... making result to be ACDB然后再次启动B ...使结果为ACDB

Is this possible in android, How ?这在android中是可能的,如何?

PS: Do I need to post code ?.. let me know ... because there are just activities and no other logic PS:我需要发布代码吗?..让我知道......因为只有活动而没有其他逻辑

You can achieve this using ActivityManager. 您可以使用ActivityManager实现此目的。 Below is the same code, 下面是相同的代码,

ActivityManager m = (ActivityManager) ctx.getSystemService( ctx.ACTIVITY_SERVICE );
List<RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(10);
Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
while(itr.hasNext()){
    RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
    int id = runningTaskInfo.id;
    CharSequence desc= runningTaskInfo.description;
    int numOfActivities = runningTaskInfo.numActivities;
    String topActivity = runningTaskInfo.topActivity.getShortClassName();
}

This will get you all the activities. 这将为您提供所有活动。 For more detail, refer docs 有关更多详细信息,请参阅文档

https://developer.android.com/reference/android/app/ActivityManager https://developer.android.com/reference/android/app/ActivityManager

Problem: Changing the backstack ABCD to ACDB . 问题:将Backstack ABCD更改为ACDB

Solution: From Android Developer documentation. 解决方案:来自Android Developer文档。

FLAG_ACTIVITY_REORDER_TO_FRONT FLAG_ACTIVITY_REORDER_TO_FRONT

 public static final int FLAG_ACTIVITY_REORDER_TO_FRONT 

If set in an Intent passed to Context#startActivity, this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running. 如果在传递给Context#startActivity的Intent中设置,则此标志将使已启动的活动在其任务的历史堆栈中已经运行时被带到其前面。

For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified. 例如,考虑一个由四个活动组成的任务:A,B,C,D。如果D调用具有解析为活动B的组件的Intent的startActivity(),则B将被带到历史堆栈的前面,使用此结果顺序:A,C,D,B。如果还指定了FLAG_ACTIVITY_CLEAR_TOP,则将忽略此标志。

In Your Base Activity Make an ArrayList of Type Activities. 在您的基本活动中创建类型活动的ArrayList。

public static ArrayList<Activity> activityArrayList = new ArrayList<>();

while changing activities just add a line in next activity 更改活动时只需在下一个活动中添加一行

BaseActivity.activityArrayList.add(this);

these activities are add in your arrayList, now you can easy add or remove any activity on your choice from array list eg i have create multiple function in Base Activity removing all activities or removing all except main 这些活动会添加到您的arrayList中,现在您可以轻松添加或删除您从数组列表中选择的任何活动,例如我已在Base Activity中创建多个功能,删除所有活动或删除除main之外的所有活动

public static void finishAllExceptMain() {
  try {
    //Constants.logMessage(Constants.TAG, "outer Start Size: " + activityArrayList.size());
    int count = activityArrayList.size();
    while (count > 1) {
      count--;
      activityArrayList.get(count).finish();
      activityArrayList.remove(count);
    }

  } catch (Exception e) {

  }
}

public static void finishLastActivity() {
  int lastActivity = activityArrayList.size() - 1;
  activityArrayList.get(lastActivity).finish();
  activityArrayList.remove(activityArrayList.get(lastActivity));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM