How to finish multiple activities on a button click? - android

I am implementing android application which is on web service. I'm creating login Activity A then homepage Activity B and so on.
Suppose there are A, B, C, D and E etc. activities. I'm creating a home button in Activity E and by clicking on that Activity E, D and C should finish and home Activity B should resume.
How can I implement that?
A > B > C > D > E > back button in E
Activity > B.

Take a look at the FLAG_ACTIVITY_CLEAR_TOP flag.
In essence it does the following. If the Activity this Intent is directed to is paused in your current back stack then it will resume it and clear the stack above it destroying all the activities in the stack above it.
UPDATE: In response to Jason Hanley.
Thanks for mentioning it. The documentation of FLAG_ACTIVITY_CLEAR_TOP covers that example. If you don't want Activity B to be recreated but just passed the new Intent then you should define its launch mode as singleTop.

Use onActivityResult method in chain . Start closing with the outermost activity-E when pressed on home , then check for its result and accordingly close activity-D and so on .

Related

Android Activity history tracking and clearing partially

Imagine this activity history stack:
A > B > C > D > E
scenario 1:
If the user is on E then on tapping the back button it should navigate to D > C > B > A.
scenario 2:
If the user is on E then on tapping a custom button "Show B", then it should clear E > D > C. Which is similar to Finish().
Like X > Y if we set finish on Y the X will be displayed. Similar If I tape Show B on E then E > D > C should be cleared from the stack.
I need to achieve both scenarios.
(Edited ^^^^ with scenarios)
If the user is on E activity and wants to move B. If B is in history stack can we clear C > D > E so that user can navigate to B without startActivity(B). and A should be in history.
If an activity is available in the stack then it should load from history if not startActivity(B).
If I use FLAG_ACTIVITY_CLEAR_TOP/FLAG_ACTIVITY_NEW_TASK, it will clear full history and start's new activity.
I want to clear partial history.
Will it be possible to achieve? If so, how to do it please?
This is all pretty standard. Don't use any special launch modes. Normally, pressing BACK will just finish the current Activity and drop you back into the previous one.
For this case:
If the user is on E then on tapping a custom button "Show B", then it
should clear E > D > C. Which is similar to Finish().
In E, to go back to the existing instance of B, do this:
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
This will finish E, D and C and return to the existing instance of B.
The flag FLAG_ACTIVITY_CLEAR_TOP tells Android to clear all activities between the current Activity and the target Activity. If you don't specify FLAG_ACTIVITY_SINGLE_TOP then the existing instance of the target Activity will also be finished and a new instance will be created. If you do specify FLAG_ACTIVITY_SINGLE_TOP then the existing instance of the target Activity will NOT be finished and a new instance will NOT be created.
You can achieve this using
android:launchMode="singleTask"
in your manifest file. For more refer this documentation https://android.jlelse.eu/android-activity-launch-mode-e0df1aa72242

Android start a new activity before another activity in backstack and clear activities above old activity

I'm stuck with an Activity backstack question. Let's say I have 5 activities in my backstack: like Activity A, Activity B, Activity C, Activity D and Activity E. At some point I want the user to go to another Activity G, when pressed on the back button on Activity E. Activity G needs to be put after Activity B, so I want Activity C and Activity D removed from the backstack (otherwise the user would go to Activity D).
Current situation A --> B --> C --> D --> E
Preferred situation A --> B --> G
Now I understand I can use FLAG_ACTIVITY_CLEAR_TOP when Activity G would have been in the backstack. But the Activity isn't. Also I don't want to use FLAG_ACTIVITY_NEW_TASK because then Activities A and B would also be gone.
Another approach would be to put
android:noHistory="true"
within the manifest for Activities C and D, but this would make the user go back to Activity B every time the user pressed the back button from within Activity C or D.
Who can point me in the right direction?
You can try below
C ----startActivityForResult----------> D ---startActivityForResult--> E
handle onActivityResult with result accordingly to finish Activities, make sure its chained action calls
When you start activity from C->D you put finish();
Intent intent=new Intent(C.this,D.class);
startActivity(intent);
finish();
same way for D->G this way it is possible.

Remove Activities from Stack History in Android

So ..
let's Suppose the Following Sequence of Activities
A -> B -> C -> D -> E
if I do The Action1 in E I just want remove E from stack and go Back To D.
on the Other hand, If I do The Action2 in E, I want to remove E and D from stack and return to C
how to do that ?
the above sequence is simple implement of messaging App, so A is The Log in Activity and B is The Profile Activity and C show the Friend Request List and D show the Profile of selected person form C, and in C there are 2 button one for approved and the second for cancel requset, now if click any of them it take him to E where Yes or No to do the Opperation, if No it return to profile of Selected person , of yes it should return to C
Well what you can do is that when you go from A->B call finish(); to end activity A, same for B->C, that is end B. So by the time you reach E, only E will be active. So now when you do Action1, call Activity D, and when you do Action2 call activity C. And this time call finish(); on E. This would do exactly what you want.
Every time when you move from one Activity to another you can add this flag with the intent Intent.FLAG_ACTIVITY_CLEAR_TOP. This will clear your all previous activities so that in Activity E there will be no Activity in Stack.
If it always the last activity who is going to perform this kind of action then you can start that last activity for result. So when D is spawning E, do it using startActivityForResult (Intent intent, int requestCode). And later when you perform actions in E, call finish() for E and before doing that pass a result code to D. Based on that result you can either do nothing (so D will remain as it is) or you can call finish() on D as well.
if by stack u mean recent application then to clear recent application you can add
android:excludeFromRecents="true"
inside your activity tag in manifest.xml
If Action1 do nothing since if a user press the Back button, E will be removed and you are back to D. If Action2, start C with flag Intent.FLAG_ACTIVITY_CLEAR_TOP, the D and E would be cleared from the stack

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

How to kill sub activities and bring activity to top of stack

I have activity A that starts activity B which starts activity C:
A -> B -> C
When a user clicks on a button in activity C, I want to bring A to the top of the stack and take B & C completely out of the stack. Is there a way to do this?
You can use the FLAG_ACTIVITY_CLEAR_TOP flag on the intent to restart activity A.

Categories

Resources