setResult() of Activity not clear - android

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?

Related

How to prevent to relaunch specific Activity after pressed back button?

When I pressed the back button on B Activity to go back A Activity, B Activity is relaunched 'cause B Activity was launched from intent.extra at onCreate of A Activity.
The intent.extra is recreated with previous data, so it happens recursively.
My intention is to load B Activity from FCM and then back to the A Activity when the user pressed the back button.
I already tried to remove data of intent after startActivity but it didn't work.
But If I check savedInstanceState is null or not, it works but I'm not sure it's the right way or not.
Plus, the A Activity's launchMode is singleTop but it's not the point of this problem, I think.
Whole scenarios are the following.
Get the data from FCM in the background and then the data is served from the System tray.
Check if there is data or not at onCreate of A Activity and then if there is launch B Activity.
Pressed back button on B Activity but B Activity relaunched at onCreate of A Activity.
How to prevent the action of 3 in the scenarios?

Activity.setResult(int) and Activity lifecycle

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.

Android sdk activity flow

Can someone help me replicate the following scenario:
I'm having a activity A with some specific tasks from which the app goes to activity B. When the user goes to B and presses back the app should close (maybe meaning that activity A performed finish()). But in some cases the app (not on the user action) goes back to Activity A, with the state preserved to when it went to B. The thing is that the app can never know if it should preserve activity A to be re-displayed later or not.
Hope I made it clear. Many thanks
You can start the Activity B with startActivityForResult() and in function of the result, when we come back to the activity A, Activity A finish() himself or not.
You can populate your Activity A every time in its onResume(). For persistence you can use SharedPreferences.
You can setup a BroadcastReceiver in Activity A which will call finish() for Activity A upon receiving a message. In the Activity B just before you close you then send that message to Activity A's broadcast receiver.

startNewActivity many times lifecycle miss understanding

in oncreate of activity A
while(i<5){
startActivity(new Intent(this, ActivityB.class));
Log.v(tag,"activity A");
}
in activity B inside oncreate
Log.v(tag,"activity B");
finish();
and then in the console I see something like this
activity A
activity A
activity A
activity A
activity A
activity B
activity B
activity B
activity B
activity B
What should I change to have situation where the output is like this
activity A
activity B
activity A
activity B
activity A
activity B
I mean for every startActivity method I want new activity to actually to be started. Cause the activity B actually lives for a fraction of moment it is started and than finished. My problem is in the understanding the activity lifecycle. I expected that when I call srartActivity the activity to be started indeed, but it doesn't happens.
Edit1:
ok I pasted just a small snippet of my code. and the problem is not in the while loop, in my real project is wrote correctly with initialization of the i variable like int i-0; and incrementing inside the loop like i++; . . . As you can see the output from the app is good, everything is get called in a sertian number of times and so on, but the problem is the order of calling. it is like startActivity doesn't start the activity after the call of the srartActivity, instead every thing is started after oncreate of activityA is finished.
This is not just about activity lifecycle, but also about the message processing in the main thread (=UI thread) of your app.
startActivity returns immediately after sending the intent, so the while loop fires 5 intents to start Activity B within a very short time before returning control to the thread's message loop.
Only after returning from the current method call, your applications main thread can start processing the previously generated messages that will result in creating the 5 instances of Acitvity 5, once again one after the other.
If you want Activity A to hold off until Activity B has finished, you can call startActivityForResult - once!. Then, in Activity B, call finish( int) to return a result to Activity A. Override onActivityResult in Activity A to get the result code and from here you can again start Acitity B if needed.
See also Starting Activities and Getting Results

Activity result not being remembered?

I have the following scenario (note that activity A has launchMode="singleTop"):
Activity A calls startActivityForResult() on activity B;
Activity B calls startActivity() on C, after which setResult(RESULT_OK) and finish(); at this point, onActivityResult() in A is NOT called.
Activity C calls startActivity() on A using FLAG_ACTIVITY_CLEAR_TOP and finish();
This is where my problem occurs. At this point, A's onActivityResult() is called with the right requestCode, but something else than RESULT_OK as the resultCode. I was expecting it to receive RESULT_OK because I have set it in B (which was started for result).
Why am I getting something other than RESULT_OK?
Read this doc:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
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.
Passing clear top flag will finish all activities in the stack. So what you'll get in onActivityResult is probably a "notification" that the activity for which you want the result was canceled. (aka RESULT_CANCELED = 0x00)
UPDATE
When I use startActivityForResult, after setting the result, I always finish my activity and let the caller come to action.
You are doing something less common: you set the result, finish your activity but you also start another one. I don't know is the expected behavior in this situation. Can't you change the interaction between the activities?
You could try to call finish() first and then start the new activity (do this in activities B and C). Anyway, I also don't know what should happen when you do this. I recommend you the first approach (changing the interaction so you don't return a result and create a new activity at the same time).
Perhaps you could chain two startActivityForResult or your activity B could send a new intent to A instead of returning its result?

Categories

Resources