How to resume an activity? - android

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

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.

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

Finishing activities when new activity is started

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

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

Categories

Resources