I have an application that have three activities, lets call they A, B and C for convention.
A calls B with StartActivity.
When user hit the back/cancel button, I have to call Activity C, so I implemented in OnPause of Activity B to call Activity C and I need return from activity C, so I called Activity C with startActivityForResult and implemented the method onActivityResult in Activity B to get the return.
Everything is working fine, but when activity C finishes, the application is getting back to Activity A, and I need Activity B.
I have to call Activity B explicitly again or I made something wrong?
I'm really not sure what you want to achieve with this behaviour. Anyway, you get back from C to A, because you pressed the Back key, and didn't override it's behaviour in onBackPressed(). So the current Activity (B) just got finished, hence onPause() was called, so C started. But by the time C becomes active, you'll only have A and C on the Activity stack.
You need to override onBackPressed() in Activity B, and call C from there, forget onPause().
You shouldn't call another activity on hitting back button. Back button will pop activity(B) out of the stack. When you clicked back from Activity B Android will finish that activity and kill it. This is a standard workflow which better do not mess up. Place some button in activity B and call C for result from there, then you will be able to get a result in B activity.
When you get to the onPause() in activity B, it is already shutting down, so when you return to it, it will be gone.
You can either, as you say, start B from C when done, or start C in B's onBackPressed() (and then return from that method without calling the super method). This overrides the default action to shut down the Activity.
Related
Hey guys I have 3 activity,
Activity A -> Activity B -> Activity C
This flow I want to perform in my application. I want this scenario in my Activity C
Scenario 1
On Back press from Activity C to open Activity B then again back press then open Activity A
Scenario 2
When something submit on Activity C, I want to open Activity A and I want to hide Activity B
So how can I do this through startActivityForResult. I check through this stackoverlow, but nothing works. Thanks
I'm assuming that the 3 activities are opened in order A -> B -> C, so they are in the backstack in that order. You can check the return values from Activity C, and act accordingly.
In Activity C, setResult(Activity.RESULT_CANCELLED) during onCreate(). That will make it the default return value when activity C is closed (back press or otherwise)`.
Then setResult(Activity.RESULT_OK) whenever something is submitted. After that, you can fininsh() that activity.
Whenever Activity C get closed, in Activity B onActivityResult(), check the return value. If it's RESULT_OK, call finish() (which will return to Activity A). Otherwise, do nothing, so Activity B will stay open
I have 3 activities to navigate. like Activity A, B, C.
From A to B i called startActivityForResult() method and overwrite onActivityResult() method. In B activity onBackPressed() i set the setResult() method and it is working fine.Now from B to C activity I again called startActivityForResult() and overwrite onActivityResult() in B and in C when back pressed I set setResult() and then call finish().
Now the issue is when I back pressed from C it will directly going to the A. I want the back navigation from C-->B--->A
Kindly help me to achieve this.
Dont call finish() in Activity C.
onBackPressed() finishes your activity. Beacuse of finish() you are going back one more step.
Remove call to finish() and everything should work as you expect.
Does Activity keep the result after Activity.setResult(int) was invoked and device was rotated before pressing back button (Activity was recreated)?
It seems that Activity does not restore this kind of information. If that is a case, should I restore the result using a Bundle and invoke setResult again?
In a word, yes. The result isn't returned until you call finish(). setResult() presumes that the Activity setting the result (call it Activity B) was started by a call to startActivityForResult() from another Activity (call it Activity A). The presumption is that Activity B will finish, resulting in an automatic return to Activity B.
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..
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();