Finishing activities when new activity is started - android

Supposing I have an Activity Stack like this(from top to bottom):
D - C - B - A
So the activity D is on top of the stack and is currently running (displayed).
Now, from the activity D I want call the activity B (in the second position in the stack), so:
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
After this operation, the new configuration of the stack, now should be:
B - D - C - B - A
But what I really want is the following stack configuration:
B - A
How can I obtain this result? Is there any FLAG that I can use for this?
Summarizing I want that when I start the activity B (from D) both D and C are automatically finished.

That is what FLAG_ACTIVITY_CLEAR_TOP for.
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
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.

you can use startactivity for result when starting d and send intent value as "ActivitStarted","D"
onresult
check result activity send d if d send then again start c
check result activity send c if c send then again start b
check result activity send b if b send then again start a

Call "C" from "D", with a intent variable like
Intent intent = new Intent(this, ActivityC.class);
intent.putExtra("BYPASS_ME", true);
startActivity(intent);
In ActivityC.java, get the intent value, if its true then finish the activity. Now C will be removed from stack.

you need to use "startActivityForResult" and then when you want to "call" activity B you actually "finish" activity D. From there, do a check in Activity C to see what you should do next:
How to manage `startActivityForResult` on Android?
You will want to pay attention to the part where you set data in your intent - that will tell you if you should finish Activity C or something else.
RESULT_OK and RESULT_CANCELED don't tell you much, and also be careful because hitting "back" can result in a null intent (so you can't rely on checking for the contents of the result intent, if that intent is null).

Unfortunately there is no flag to fulfill your requirement.
As Tober said:
What you can do is you just finish C before D and do same when calling the next one.
In my view the first answer is same as tober like call finish in both C and D but now in A when you acivities are going in onPause stage .

How about this
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
finish();

Related

How to startActivityForResult with a medium activity?

I have 3 activities A, B, C. I need to call A -> B -> C -> A. Activity A needs result from C but compulsorily calls B.
I did try below, but it does not work!:
A calls startActivityForResult(B), B calls startActivityForResult(C)
then B.finish(), C has returnIntent to A then finish().
Thank you very much for any suggestion!!!
You can start B for result and from B start C for result. When C finishes it must give you either (Your wanted result or not), Then in B's onActivityResult (check whether C given you your wanted result or not If yes finish B as soon as you receive positive result) and send result back to A. That's simple. –
Instead of doing by this way you can send data using intents. & when you are on activity C, so whiling moving to activity A, you can just clear activity stack top, it will solve your problem same time your data will be passed from activity C to activity A.
Like below :
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("data", "data");
startActivity(intent);
finish();

Activity history Stack is not getting clear

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();

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

How to resume an activity?

I have 3 activites a, b and c. This is how they work:
a->b->c
a starts b, b starts c. What I want to do is, I want to resume c from a, if c is running. I don't want to restart c. I cannot just pass an intent, as c needs some extra data in the intent to start again. So is there a way to resume c from a?
Resuming c from a means there should be something like: a=>b=>c=>...=>a
in this case:
try:
Intent i = new Intent(a, c.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
this will destroy every class after c=>.... and will return to c.
You can bring the activity back to front by using a flag on the intent FLAG_ACTIVITY_REORDER_TO_FRONT

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