Activity history Stack is not getting clear - android

I have 4 Activity classes e.g A, B, C and D here i am going to B from A and then C and in the menu of this(C) activity i have given a link for Activity A so when i am selecting that menu i am going to A and again from A i am going to B from here i am going to D and come back to B and then going to C.
So overall scenario is A->C->A->B->D->B->C
now the problem is while pressing back button it goes like this
C->B->D->B->A->C->A exit
but i want to use like this if users in activity A its directly close the app no need to go back to B and then C but if its in B then no need to go back in C Activity.So for the C and D i am using android:noHistory="true" and its working fine but i can't use this thing for B. because while coming back from D to B , i can't skip B Activity and directly jump to A.
So now stack is like this:
C->B->A->A exit
Following that i have tried is:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Thanks for considering.

Where you use Intent it stores the previous activity in the stack so use finish method to clear the stack like example
Intent intent = new Intent(A.this,b.class);
startActivity();
finish();

this code will help you
Intent intent = new Intent(this, FirstActivity.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

Related

How to clear current activity stack when start another activity which belong to another stack

For example:A start B, B start C, A and C belongs to 'Hello'-stack, and B belongs to 'World'-stack, when user press back button in Activity C, it will return to A.
Note1:B means a lot of activities , not just one activity,
like A start B1,B1 start B2, B2 start B3....,Bn start C.
Note2:I need Bs remain in stack until C has been launched , when user press
back in B3, it should return to b2.
I actually have implemented this need by using startActivityForResult,and I just want to know is there any way using stack to implements this.
When going from activity A to activity B, go as below:
Intent i=new Intent(ActivityA.this,ActivityB.class);
startActivity(i);
And as you want to open C from B but don't want B to be in stack, so do as below:
Intent i=new Intent(ActivityB.this,ActivityC.class);
startActivity(i);
ActivityB.this.finish();
So automatically when backpressed on C, you will get A and not B.
Simply call finish() when you start Activity C from B.
Then override onBackPressed() in activity C. And add code to start Activity A.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
or
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
works for me.

Android Stack management and bring older activities on top

I have activity A that starts activity B which starts activity C and this starts D
A -> B -> C -> D
When a user clicks on a button in activity D, i want to bring B to the top of the stack and take D & C completely out of the stack. But A shown be there in the stack and when user clicks back in B s/he should be directed to A. Is there a way to do this ? ?
Yes It is possible.
You should read this topic. Use FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
You can override onBackPressed() to navigate to the desired activity without a hassle.

Android exit from previous activity

I have 3 activities in my application
ActA
ActB
ActC
Suppose I am in activity ActB and I am loading ActC with out finish(); ActB
Then when press a button in ActC , need to redirect the application to ActB . But this time when I press back from redirected ActB , another ActB ( previously loaded ) is showing.
Is there any way to kill all the activities which are previously loaded when we press the button in ActC ?
I am new to android and its ruining my time
Please help
thanks in advance
When you launch ActC from ActB, do so with this flag on the intent:
Intent intent = new Intent (this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Javadoc:
"If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent."
Just going from ActB to ActC, use Intent and finish() after calling the Intent
Intent intent = new Intent(this, ActC.class);
startActivity(intent);
finish();
And then if you want to go back to B from C, then do the same in reverse, so switch
Intent intent = new Intent(this, ActB.class);
And the rest is the same.
Suppose you move like this
A -> B -> C
All the previous instances will be there in backstack for previous activities.
until and unless it is your requirement to create new instance of activity then only do so.
when you press button in you want to come to B but if you don't need new instance of B you can go with backstack item and according to me you should.
in button click you can simply call onBackPressed() of activity which is called when you press back button of device.
Also as Vee said you can use that flag to clear activities above your current activity.
If you want to "kill" the activity you should call finish();
To achieve your goal you can do the following thing.
When starting ActB from ActA, after calling startActivity(...); put finish();
This way you killed Activity A, do the same in ActB when calling ActC. Then when you call ActB from ActC again, it will start a completely new activity.
If you don't need a new instance of B then you can simply call finish() in your onClick() of C and this will take you back to B and no need for Intent or any other code.
If you need a new instance of B then you can use Vee's suggestion, keeping in mind that this will clear Activities off of the stack if you add more in between.
If you don't need a new instance of B but want to pass data back to it then you can use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(ActC.this, ActB.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// can send data to ActB if needed with putExtras()
startActivity(i);
finish(); // if you want to destroy C and take it off the stack
this will not create a new instance of B but bring it to the top of the stack so when you press the "Back" button, you will not have the second instance on there.
When user presses button in ActC to goes back to ActB (by creating a new ActB) do this:
Intent intent = new Intent(this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will finish both ActC and the previous ActB and create a new ActB.

Start Activity and don't destroy other activity

I am new to Android and started the activities A - B - C - D. From activity D, when I open activity A again then how can I start so that activities B and C don't finish and A starts again? There should only be one activity A.
Thanks in advance.
Use the Intent Flag FLAG_ACTIVITY_REORDER_TO_FRONT
In D
Intent i = new Intent(ActivityD.this, ActivityA.class);
i.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
This will simply bring ActivityA to the front of the stack and leave B and C where they are which I believe is what you want. Then you can obviously call finish() on D if you want to remove it from the stack.
You can find all available flags in the Intent Docs

Android:Delete a activity in backstack

I have got 4 activity let it be A->B->C->D.In every A,B,C activity user need to enter data all data will sent to server in C activity if the user data is correct he will move on to D activity and all the activity A,B,C removed from stack.If the data is in correct i need give the user to reenter data i.e is on back press it has to move C->B->A.My question is How to remove A,B,C activity when user enter D activity.
Use FLAG_ACTIVITY_CLEAR_TOP this shall solve your problem
From the Android documentation:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
For example, 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 receive
the given Intent, resulting in the stack now being: A, B.
Use it like
Intent intent = new Intent(getApplicationContext(),
yourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and also, take a look at this question:
Activity with Flag FLAG_ACTIVITY_CLEAR_TOP (android)
Edit : I thought you want to move to your home activity from D and want to remove all activities from stack
Your stack would be like homeactivity , A , B , C , D
so i gave you this solution as this shall remove all activities in stack on top of your home activity.
If you want to clear the the stack while going to D, for that you can use FLAG_ACTIVITY_TASK_ON_HOME or FLAG_ACTIVITY_CLEAR_TASK
But both of these for api level 11.
The correct answer interesting for me also, but I can offer a solution:
For example you start activity A from O: O->A->B->C->D.
On activity O you can put in android manifest android:launchMode="singleTop"
Then, when data are ok, you can start activity O with flag "FLAG_ACTIVITY_CLEAR_TOP" - it remove from stack A,B,C and will be called method onNewIntent (Intent intent) http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) in O, where you can start activity D.
You can start activities with startActivityForResult, and call setResult from for example activity D, in C activity you can listen activity result, and related of this result finish activity or not, or call setResalt from C activity ...
When you want to move D activity there you need to check your
conditions and if your condition is satisfied then you need to enter
into your Next activity(i.e., D) .In that case you need to use the
following code..
Intent intent = new Intent(this,D.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Suppose In on backpress you need to use finish(). to move back i.e., C
-> B -> A.
Try this piece of code with some modifications:
Intent intent = new Intent(this, D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
startActivity(intent);
Try looking for Activity's public method startActivity(Intent i) and finish() here
In usage wise, it should look like this.
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
finish();
Hope this helps :D

Categories

Resources