Activity.setResult(int) and Activity lifecycle - android

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.

Related

Resume Parent Activity

I have 2 Activity A and B, A calls B so, I want to Resume parent Activity (A) when B calls finish() on its Activity. Any advice will be useful.
UPDATE:
Maybe I should mention that I use fragments, each Activity has its own fragment, I call finish() from fragment hosted inside B activity and I expect to receive Resume on fragment belongs to A.
When you call Activity B, Activity A will go to background. Unless you finish Activity A while starting Activity B, it will automatically resume when Activity B finishes.
If you are calling this,
startActivity(ActivityB.class, this);
finish();
Just remove the finish(). It should work as expected.

setResult() of Activity not clear

I am new to Android. I got a scenario in my App as follows :
Imagine activity X starts activity A by startActivityForResult.
Then A starts activity B by setting Flag_Activity_Forward_Result.. But A is not finished.. It is in onPause state.
If B stops by calling finish(), A which is in background vil come to foreground.
How can we handle setResult() now?
Will setResult() in A serve the purpose of sending result to initial caller Activity X?

Who gets the Activity result when an Activity is re-constructed on configuration change?

As I understand it, if I don't explicitly tell Android that I want to handle a configuration change in an Activity, it will get torn down and re-created on, say, an orientation change.
Let's say that I have object O that's an instance of some class MyCustomClass in Activity A. When the user presses a button in Activity A's layout, we call O.startActivityB. That method will start Activity B. When Activity B gets re-created on an orientation change, I can tell (some flag was set in another or something), so Activity B immediately runs this:
setResult(RESULT_CANCELED);
finish();
return;
Who is Activity B's parent activity at this point? Who gets the result code?
If you started Activity B from Activity A, the 'parent' is Activity A. The fact that an activity gets recreated for some reason doesn't change the activity stack in any way. BTW, if you want to receive a result, you need to use startActivityForResult(), not startActvity().

Activities lifecycle in Android

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.

where will android finish() return to

I have an activity than can be called from my parent activity and from other application via intent filter (i.e. ACTION_VIEW).
When I call finish() inside my activity, how to return to the correct caller?
i.e:
other application -> my activity -> finish() -> other application
currently if my main activity is still running, the finish() will return to my main activity, although it was called from other application.
If you start one activity with startActivityForResult(Intent) then you can get the second activity which started the first one with getCallingActivity().
If you call the finish method on your activity, you will be returned to the topmost activity in the history stack. By default, it is the previous activity that you accessed.
For example:
Main > A > B
If finish is called on Activity B, you will return to Activity A.
If finish is called on Activity A, but Activity B is still alive, you will stay on Activity B since it is the topmost activity in your history stack.
If finish is called on Activity B, but for some reason Activity A is not in your history stack (most probably, if you specified that Activity A must not be saved in the history stack), you will return to Activity Main instead of Activity B.
Read up on Tasks - by calling the activity from a different task (/application), you're bringing the existing Task to the foreground, which might include other Activities in the back stack that you don't want. I would suggest specifying the android:taskAffinity of the Activity that can be started from other Applications. Since this will ensure that this Activity will be the only one in the Task, it doesn't matter if it is started from within it's own app, or another.
EDIT: Figure 4 in the Tasks link shows your situation.
Use startactivityforresult() when you want to call the child activity. You'll return to the good one.

Categories

Resources