How to check if all activity is on the stack?

What is the best way to check if all the activity on the stack is to call back?

Intent i = new Intent(getApplicationContext(),MyClass.class); startActivity(i); 
+5
source share
2 answers

Check out the ActivityManager API

To get an instance of an ActivityManager, use this code:

 ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE ); 
-1
source

I wonder how unpopular this question is.

Let me start with a solution first:
Since ActivityManager.getRunningTasks deprecated since API 21,
We have to find another way to get what actions are in the back. And I realized that we can really implement our own "stack"!

I declared ArrayList in MyOwnApplication:

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

And public methods have been added 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 BaseActivity , where all actions 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 for validation.

WHY DOES IT NEED?

Yes, of course, most cases can be done using Intent flags and proper navigation.
But there is such a precedent, which, it seems to me, should be general, that I can’t find a solution simply using the flags of intent.

Suppose I have an application that has a navigation box in almost every action.
I move from MainActivity to ActivityA , and then create a ChildActivityB from ActivityA . Note that ActivityA not a parent of ChildActivityB , since ChildActivityB can be opened from other actions, such as notification.

Please note that ChildActivityB also has a box. I can go to ActivityA through the box, instead of pressing the up or back button. Now imagine that you are doing this process: Activity A β†’ ChildActivity B β†’ Drawer β†’ Activity A β†’ ChildActivityB β†’ Drawer β†’ Activity A ..... In the back endless actions will be created.
To fix this 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 using overridePendingTransition() . I noticed that if I add the above intention flags along with overridePendingTransition() , the animation will crash because the action will be destroyed in the middle of the animation due to the Intent.FLAG_ACTIVITY_CLEAR_TOP flag.

Now, if I can determine if ActivityA in the backstack or not, the behavior would be ideal:

 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); } } 
+1
source

Source: https://habr.com/ru/post/981617/


All Articles