How to clear all actions in an Android application

In my application there are many actions that can be called in any order

Example operation history: A โ†’ B โ†’ C โ†’ D โ†’ A โ†’ B โ†’ E

Now in step E, I โ€œunregisterโ€ the device (by registering the user and deleting any data that they could download to their SD card). The behavior of the desire is that the application "starts", and the user is prompted to activate the input activity, and clicking on it returns the user to the main screen.

So now, activity E must somehow clear the activity stack. I am currently setting FLAG_ACTIVITY_CLEAR_TOP when starting an intent with E. The problem is that when the user visited A and then switched to intermediate actions and moved to A before moving to E, there are still actions on the stack.

A โ†’ B โ†’ C โ†’ D โ†’ A

Thus, the user is logged out and cannot use the BD actions, but if the user deviates from activity A, they can access the BD actions. Is there an easy way for all actions except login activity to be removed from the stack?

Update:

So, I tried updating my BaseActivity (every action in subclasses of my application) to contain a static isDeregistering flag that tells the activity to destroy itself if true. The problem is that every action calls finish (), and I boot to the desktop and cannot restart the application until I forcefully close the application. Is there a better way to do this?

+3
source share
2 answers

I figured out a workable solution. The following code is in my BaseActivity class.

public boolean killIfDeregistering() { if (isDeregistering) { Log.d(TAG, "deregistering true!"); if (getClass().getName().equals(LoginActivity.class.getName())) { //don't destroy activity, reset flag Log.d(TAG, "stopping deregister process!"); isDeregistering = false; } else { //finish the activity Log.d(TAG, "killing this activity!"); finish(); return true; } } return false; } 

Using the Reflection bit, I can decide whether to kill the underlying activity so that the home launcher can restart the application in LoginActivity . I just have to make sure LoginActivity will not stay on the stack after it has logged in, calling it finish() manually.

+3
source

If you want to return from activity E to the first activity of A, having destroyed all the actions of the intermediates, you can start your intention with FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_CLEAR_TOP

0
source

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


All Articles