Android activities question - android

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

Related

Close parent activity and launch a new one

I have a base activity and some child activities like A, B, C and D. Here A is parent of B, C and D means that when either B, C or D finishes, control comes back to A. Now what I want is that when Activity say D finishes, it also close A (parent) and launch a new one say E. Here E should be the only on stack.
Its mean close all the activities that are invisible or waiting for result and launch a new one (E).
I don't know if this will work, but you could try it:
From Activity A, start activity B for a result using startActivityForResult()
set a result that will inform A to finish as well,
Call finish() in B.
When A receives that result from B, A calls finish() on itself as well.
and there you can start another activity.
for this you can set the request code for more than one activity which was start as for result and check into onActivityResult() to compare the result code
Try to register all the activity for broadcastreceiver.. When D is going to finish let it Broadcast, by listening to this finish all the activity and start new one before finishing anyone of the activity..

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.

Android and popping from Activity C to Activity A or B

I have three activities in my activity stack, A (main) -> B -> C.
A starts B starts C.
My C is a dialog box with one button that will take me to A and one button that will take me to B. The Activity B must therefore have history, so if C calls finish() I will always end up in B.
Given this, how do I (efficiently) set this up? i.e. how do I (efficiently) go from C to A?
I assume that you want to finish() B if the user selects the A button on C. Have B start C for a result (startActivityForResult()) and have C send back to B the result. If the result is "go to A", then B can just finish().
One way is to set up the onActivityResult method in activity B.
Based on the button clicked, return from activity C with different return codes, which you can access in the onActivityResult method of activity B. Based on the return code, you can either stay on the activity B, or close the activity which will take you to activity A.

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

How to clear current activities in the stack?

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.

Categories

Resources