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.
Related
I have few activities.For example A,B,C,D,E,F.
My Home screen is activity A.Now I start Activity B from that and C from Activity B.
So now my stack is A->B->C.
Now if I press back I should be able to navigate in reverse Order.
But If I start Activity D from C.
I want my stack to be A->D as I want to kill B and C.
My expectations is A is my Home screen which should be always there.B and C on having one task and D,E,F having other task.
There is one more catch - I can even start activity D->E->F from A and B from F. In that case when I start B from F, D,E,F should be removed from stack
There are several ways to accomplish this, depending on your application requireents. You should look at using startActivityForResult() in order to start one (or more) activities which can then return a result to the Activity that launched it. This may be a good solution for you.
The other alternative is to use your ActivityA as a kind of "dispatcher. In this case, when C wants to launch D but also clear the task stack back to A first, you can do something like this in C:
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("launchActivity", "ActivityD");
Using the flags CLEAR_TOP and SINGLE_TOP will cause all activities on top of the existing instance of ActivityA to be finished and cause the Intent to be routed to onNewIntent() of the existing instance of ActivityA.
Now, in ActivityA, override onNewIntent() with something like this:
if (intent.hasExtra("launchActivity)) {
if (intent.getExtra("launchActivity").equals("ActivityD")) {
Intent launchIntent = new Intent(this, ActivityD.class);
startActivity(launchIntent);
} else ...
}
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.
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();
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
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