how to check if activity is still in the stack?

孤街浪徒 提交于 2019-12-30 08:58:08

问题


what is the better way to check if the activity is still in the stack in order to call it back ?

Intent i = new Intent(getApplicationContext(),MyClass.class);
startActivity(i);

回答1:


Look at the ActivityManager API

To get an instance of the ActivityManager use this code:

ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );



回答2:


I am surprised how unpopular this (kind of) question(s) is.

Let me start from the solution first:
Since ActivityManager.getRunningTasks is deprecated since API 21,
We have to find another way to get what activities are in the backstack. And I realized that we can actually implement our own "stack"!

I declared an ArrayList in MyOwnApplication:

private ArrayList<Class> runningActivities = new ArrayList<>();

And added public methods to access and modify this list:

public void addThisActivityToRunningActivityies (Class cls) {
    if (!runningActivities.contains(cls)) runningActivities.add(cls);
}

public void removeThisActivityFromRunningActivities (Class cls) {
    if (runningActivities.contains(cls)) runningActivities.remove(cls);
}

public boolean isActivityInBackStack (Class cls) {
    return runningActivities.contains(cls);
}

In a BaseActivity where all activities extend it:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyOwnApplication)getApplication()).addThisActivityToRunningActivityies(this.getClass());
    }

@Override
protected void onDestroy() {
    super.onDestroy();
    ((MyOwnApplication)getApplication()).removeThisActivityFromRunningActivities(this.getClass());
}

And then you can easily use isActivityInBackStack to check.

WHY IS THIS NECESSARY?

Yes, of course, most cases can be done by using Intent Flags and proper navigation.
But there is such a use case, which I think should be common, that I don't find a solution simply by using intent flags.

Suppose I have an application that has a navigation drawer in almost every activity.
I navigate from MainActivity to ActivityA, and then created ChildActivityB from ActivityA. Please note that ActivityA is not parent of ChildActivityB since ChildActivityB can be opened from other activities such as notification.

Note that, ChildActivityB also has a drawer. I can navigate to ActivityA through drawer, instead of pressing up or back button. Now, imagine you loop through such process: Activity A -> ChildActivity B -> Drawer -> Activity A -> ChildActivityB -> Drawer -> Activity A ..... Infinite activities will be created in the backstack.
To fix such behavior, we need to use Intent Flags:

(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);

So far so good. However, I have custom activity transition animations by using overridePendingTransition(). I noticed that if I put the above intent flags together with overridePendingTransition(), there will be a glitch in animation because the activity is destroyed at the middle of the animation, due to the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.

Now, if I am able to detect whether ActivityA is in the backstack or not, the behavior will be perfect:

private void navigateToDrawerItems(Class cls) {
    drawerLayout.closeDrawer(GravityCompat.END);
    Intent intent = new Intent(this, cls);
    if (((MyOwnApplication)getApplication()).isActivityInBackStack(cls)) {
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }
}



回答3:


You can toggle global variable as indicator inside onCreate() and onDestory() of specific class, OR inside onActivityCreated() and onActivityDestroyed() of ActivityLifecycleCallbacks.

e.g.:

  registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

      @Override
      public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
          if (activity instanceof YourActivity) {
              myGlobalData.setActExist(true);
          } 
      }

      @Override
      public void onActivityDestroyed(Activity activity) {
          if (activity instanceof YourActivity) {
              myGlobalData.setActExist(false);
          }
      }
  });


来源:https://stackoverflow.com/questions/13970728/how-to-check-if-activity-is-still-in-the-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!