Remove Activities from Stack History in Android - 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

Related

How to control/modify the activity stack in my case

I have following activity stack: A->B->C->D
In activity D, a new Activity E will be launched. Is that possible to change the activity stack to A->E (so if user press back they will go to activity A)?
Additional information: I don't want D,E depends on A. So override E's onBackPressed to start A directly or start A from D with CLEAR_TOP flag and then go to E is not a choice.
ok as per your requirement it is possible using startactivityforresult and onActivityResult let me explain
1.first call A to B
2.from B call startactivityforresult C, also in B add onActivityResult to handle result
3.from C call D as step 2.
4. Now when you call E from D just finish D and setResult(RESULT_OK)
6.Now in activity C onActivityResult get called there check for result ok and finish activity and setResult(RESULT_OK) same for B
here you get stack A to E.
for more info http://developer.android.com/training/basics/intents/result.html
if any problem let me know
You can manage both your stack and your back pressed buttons by overriding the onBackPressed method and by using Tasks and Back Stack or not.
So within backpressed methods:
#Override
public void onBackPressed() {
startActivity(new Intent(this, YourChosenClass.class));
super.onBackPressed();
}
A task is a cohesive unit that can move to the "background" when users begin a new task or go to the Home screen, via the Home button. While in the background, all the activities in the task are stopped, but the back stack for the task remains intact—the task has simply lost focus while another task takes place, as shown in figure 2. A task can then return to the "foreground" so users can pick up where they left off. Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity.
Activities linked with a task will be available in the back stack.

Understanding Intent.FLAG_ACTIVITY_CLEAR_TOP

As per the image shown above, I have some queries. It is requested to read each step in order :-
Each block is an Android Activity
Arrow represents the Stack Direction - the order in which activities are opened(A is started when the app was first launched)
Here when the User reaches ACTIVITY F and want to open activity Z (We are using Flag_Activity_clear_top) for the same.
After that from ACtivity Z when the user wants to open the Activity D.
****Our Requirement at this step is - When the Activity D is opened and the user do presses the back button - I WANT THAT USER SHOULD BE REDIRECTED BACK TO THE ACTIVITY C, AFTER THAT ACTIVITY B and so on..** **
Currently when we press back from the activity D(after coming from Z), then we are being redirected to the Activity Z.
CLEAR_TOP isn't good, because if you open an activity that way, it will remove the whole stack and that doesn't sound like what you want.
Try this:
When starting activity E (from D), F (from E) and Z (from F), do it with the flag "FLAG_ACTIVITY_NO_HISTORY". This flag will prevent the new activity to appear in the back stack.
Keep in mind that any activity you open this way will not be registered in the back stack. So, if you hit back while (for example) you're in F, it will return to D.
Hope this helps!
->Incase anyone is facing the same issue. Try sending the intent along with the flags 'FLAG_ACTIVITY_CLEAR_TOP' and 'FLAG_ACTIVITY_SINGLE_TOP'.
->Example mentioned in the docs: link
Consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receives the sentIntent, resulting in the stack now being: A, B.

Removing an Activity from History Stack [duplicate]

This question already has answers here:
Remove Activities from Stack History in Android
(5 answers)
Closed 9 years ago.
Let's Suppose that I have the Sequence of the following Activities :
A -> B -> C -> D -> E
when I be in E coming from D I want to do some Actions then Go back to D without Keeping the E in the Stack.. So if I have back to D and press Back Button I will return to C not E
how to do that ???
If you add this in your android manifest for activity E it will not appear in the History stack.
android:noHistory="true"
Instead of creating a new intent to go back you should call this.finish(); on the activities that you no longer want, this will be "called" when the user pressed the back button as well, also, if you are creating a new activity and you wish for the current activity to be skipped when pressing back you can still call this.finish(); and then you can call this.startActivity(new Intent(this, ActivityName.class));
I'm not sure if I completely understand this
if I have back to D and press Back Button I will return to C not E
but if I do all you have to do is call finish() when you are done in E then you will return to D, then to C when you press back in D. If this is not what you are talking about then please clarify your question. But, when you finish() and Activity then it is cleared off of the stack so you would never return to E until you start it again. You never would from D anyway by pressing the back button
Google I/O Navigation
In its most basic form, Activities are put on the stack on top of each other in the way they come in. If you leave one, by calling finish() or pressing back, then it is removed and you are taken to the one placed before it (where you came from). There is much more to that but that is the very basic of what happens.
I think this is how you did your activity stack
A -> B -> C -> D -> E -> D (launched by intent from E).
Now you want to go from the second D to C. To do that, in E, call finish() after you start D. This should remove E from the stack.
If I understood your problem correctly;
You goes to Activity-E in a sequence of A, B, C, D, E.
Now you will come at the Activity-D when you will press the back button.
And here is the problem: When you press again back button, it goes again to Activity-E but you want to go on Activity-C here.
So you can use it in Activity E
#Override
public void onBackPressed(){
finish() ; // ActivityE.this.finish()
super.onBackPressed() ;
}

Getting back to the first activity via intent, what happens to the activity stack?

I have an application which pulls data from a webserver, putting this data in a listview and then presenting it to the user. There are 4 activities involved, which can be called like this:
A -> B -> C -> D
or
A -> B -> D
Basically all the activities except A are pulling data from the web. Should there be any problems with the connection and there is a timeout coming up I want the activities B, C and D to inform the user and get back to A.
So what I did right now is, I set A to launch mode singleTask. This way I can catch the timeout exception and call a new intent starting A. But what happens to the activities in between? Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A, but what about B and C? Does android automatically call onDestroy on these? What happens to the activity stack? Any hints appreciated.
Cheers
When you launch activity A, from activity D, set the intent flag:
FLAG_ACTIVITY_CLEAR_TOP
Using this flag will clear any activities in between A and the activity you are in, bringing A to the front. You also probably don't need to be using singleTask as a launch mode.
Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A...
With your example, if all the child activities are launched with startActivityForResult() and activities B & C implement:
finishFromChild(Activity activity) {
...
finish();
}
When activity D calls finish() after it has timed out, then each child will close in order (D -> C -> B -> A) with a chance to return any relevant data you might want to salvage.

How to finish multiple activities on a button click?

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 .

Categories

Resources