How to clear current activities in the stack? - android

Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.

Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.

Related

How to manage backstack in Acitivity Stack? [duplicate]

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.

Return result to previous activity that is not the last activity

The startActivityForResult()and onActivityResult works perfect if there are only two activities are involved. But how can I handle this, if more than 2 activities exists?
Example:
Activity A starts a new activity B, that starts activity C, that starts activity D. I want to return the result of D to activity A along with finishing activities B and C. How can I do this? Can I loop through the activity stack and finish the wanted activities or must I start a new instance of activity A?
For short: A->B->C->D has to lead back to A with the result of D.
Going back closing each activity would be a good way of doing things, but if you need to jump from an activity to another and you're not using a TabHost, you could take a look at the APIDemo Reorder code
It jumps from an activity (4th) to a previous opened one (2nd) in this way:
Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
In my opinion the most logical way is have the Activities take responsibility for this.
D returns d to C.
C returns d and c to B.
B returns b,c,d to A.
This will force you to consider the error conditions when the Activities don't happen in this cycle explicitly.

Moving between activities: killing two previous activities

I'm developing an Android 2.2 application.
I'm very new on Android, and I see that if Iaunch a new Intent from an activity, this activity goes to paused state.
If I want that user can't goes back to this previous activity, what must I do? May I kill this previous activity with finish?
UPDATE
An example:
A, B C and D are activities.
A is the first activity.
A launches B, and B launches C, and C launches D.
I want to close or kill activity B and C when D is launched.
Thanks.
You can use your intent to startActivity like normal and follow the startActivity with
finish();
If you do this on your hypothetical activity B, you can hit the hardware back button in C and it'll take you directly to A.
You might want to rework your design. What if you have A call startActivityForResult(B, 0), and when B would have launched C, instead setResult and finish. Back in A, onActivityResult gets called, with the result you set. You can use that result as a message for A to now start the activity C.
See if that helps - http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP (This and other flags)
And also you must invoke
finish();

Android activities question

I have three activities, A, B and C.
User can start activity B from A, and then C from B.
I would like to have following. In C the user fill some field and click "OK"-Button. After that both activities B and C should be finished.
I don't like my idea, to finish A after starting B, then finish B after starting C, and then start A from C.
What is the best way to do that?!
Is it possible to finish parent activity from the child? If, yes, then that would be my solution. But i haven't find any information about that.
Thank you,
Mur
in Activity B, call Activity C with startActivityForResult. In the ActivityB.onActivityResult() method, call finish() when Activity C returned with a good result. This gets you back to Activity A.
try This
In Activity C Class
Intent mIntent=new Intent(getApplicationContext(),A.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(mIntent);

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