android: onActivityResult() order of execution - android

I am a newbie to android app development.So I am having confusion with onActivityResult() method.
In onCreate() method I made a button onClickListener which on clicked will call startActivityForResult for selection of images from gallery.
What does exactly happen after onClickListener is called.Does my previous activity restarts again by calling onCreate() method or resumes by calling onResume() method.But what I have seen is that still onCreate() method is responding while clicking button and taking me to gallery.
What is the exact order of execution of this one with activity life cycle?

In this case, onRestart() -> onStart() -> onActivityResult() -> onResume() is what happens after choosing a picture from the gallery.
An activity only needs to call onCreate() and assign onClickListeners once per life cycle.

Overide all lifecycle methods (like onStart, onStop...) and add yours log if you want to know what happens with your Activity
something like that:
onStart(){
super.onStart();
Log.i("MyTag","onStart");
}
and look at LogCat with tag:MyTag :)
also look at this https://github.com/xxv/android-lifecycle

The only difference from calling startActivity vs startActivityForResult is that when your first activity is resumed, you will get a callback to onActivityResult (in case the second activity called explicitly to setResult()).
Regarding the lifecycle, after going back from your second activity (by finishing the image selection or clicking back button), your first activity will be resumed and not created again. You can review Activities lifecycle in Android documentation Activity Lifecycle

Related

Why is onCreate called twice but no onDestroy called between two onCreate in my Activity?

My team wrote a complex LBS/chat App, there is a MainActivity as main displaying activity.
When this MainActivity started, "onCreate" is called the first time.
when I switch to other several Apps and go back to my App's MainActivity, "onCreate" is called second time.
But "onDestroy" of MainActivity has never been called.
Some pointers(Singleton/Global, not belong to any activity) will be "auto" set to null out of my control, it causes my app problem.
Does some module/class hold a reference to MainActivity or some other reasons?
I need help -_-!
Thanks to all guys who can give me some tips.
Thanks!
OnCreate is call only once in activity circle life.
If your application was in background and return to foreground, the only reason that on create call again is because OS kill this activity.
you can save data with onSaveInstanceState().
when you start activity this functions be called: onCreate() , onStart() , onResume() and when you going to another activity, onPause() called.
If you want to call onDestroy() to your app you must call this.finish(); but, when you call this.finish() you must handle onBackPress() in your app.
I hope this useful to you

abnormal behavior of parent activity after finishing child activity

I have Activity A and I am calling Activity B from Activity A using setResultForActivity.
Now in Activity B when I press Done button I am firing finish() and it returns to
Activity A and it return down to onActivityResult. Now the issue is after when I fired finish() in Activity B , Activity A's onCreate doesn't get called and thats why
some of the custom listeners in my ListView isn't working , it seems that they are not bind.
so the whole activity respond pretty weirdly , can anyone has solution to this ?
Why a fourth answer? Because in my view, the others aren't fully correct.
Truth is, Activity A could have been destroyed in the meantime, or not. This depends on whether Android needs memory or not. So it is possible that Activity A´s onCreate() is called (along with the other lifecycle callbacks), or not. In the latter case, onActivityResult() is called before onResume().
While for configuration changes, the most efficient way to preserve the Activity's state is via nonConfigurationState, if you want to prepare for a restart of your Activity after it has been destroyed, you can use the InstanceState mechanism, because while Android destroys your Activity A, it will keep its intent and saved instance state to re-crearte it.
This stresses the absolute necessity to place initialization exactly in the callback where it belongs.
To test whether your Activity logic works regardless of whether Android destroyed it or not, you can use the DevTools setting "Development Settings" -> "Immediately destroy activities". The DevTools app is available on AVDs and can also be downloaded from Google Play.
Just place your onCreate() stuff in onResume() of Activity A except setContentView().
Just have a read on Android Activity Lifecycle : http://developer.android.com/training/basics/activity-lifecycle/stopping.html. onCreate() is only called when the activity is first created. You can do your list thing in the onResume() method.
Activity A's onCreate won't get called because the activity has not been destroyed. When an Activity regains focus from another activity, it's onStart and onResume get called, so I would put your bound listeners in those. They will also be called when onCreate is normally called.
After your child activity is finished() it return to execute onActivityResult which is in your case in Activity A. The onCreate method is not supposed and does not get called when killing of you sub-activity, a.k.a Activity B.
Please post some source code for us to work on and I will improve my answer! :)

how do i get an activity to restart but not re-create?

Here is the scenario:
I have two Activities. Lets name them Activity A and Activity B.
Say Activity A is open. Now, when I go and open Activity B, Activity A is closed because the onStop() method is called.
Now, when I flip back to Activity A, the onCreate() method is called, but I want the onRestart() method called instead. How do I do this?
You cannot influence the livecycle of your app like that. There should be no reason to rely on onRestart(). If you use onStart() it will always be called no matter if the Android OS killed the app process in the background.
Check out this doc for further information:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Damn beat me to it but here goes anyway
According to the Activity Lifecycle onCreate() is called again if the Activity was removed from memory because the OS deemed that another app needed the memory. In this case, you can't ensure that onRestart() will always be called for your Activity.
Like already stated you must find a different way of achieving your goal by using the other Lifecycle methods such as onStart() or onResume
I'm not sure if it fits your needs, I had to do an update service that starts the first time I open ActivityA (main Activity) and stops when exiting from ActivityA (not returning back from ActivityB),
I've placed the "start code" in onCreate() when savedInstanceState is null and the "stop code" in onDestroy() if isFinishing() is true

Using OnPause and OnResume in an application

I am making an application in which i want to let the control go to some activity and then to come back. When I have searched about that problem, I came to know that this problem could be solved by using OnPause() and OnResume(), but I don't know how to use them.
This is the code where I am transferring the control to some other activity. Please tell me how to use OnPause() and OnResume() in this regard.Thanks in advance.
Intent intent = new Intent(MCQ.this,ConfigActivity.class);
int ClassIndex = 2;
intent.putExtra("ClassIndex", ClassIndex);
startActivity(intent);
You are not making too much sense. When you call startActivity(), Android will put your current Activity to the background, and start (or bring to foreground) the new Activity specified in your Intent.
onPause() called when your Activity disappears from the screen, eg. the system either puts it in the background, or terminates it.
onResume() is called when your Activity becomes visible on the screen, eg. either after onCreate() if Android created a new instance of your Activity, or when the system brings your Activity back from the background.
There is no explicit passing of control between Activities. If it is on the top of the activity stack (visible on the device screen), than you can interact with it.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
onPause() and onResume() are called by android.
Your job is to implement these methods. onPause and onStop are called at your launching activity by Android, and onCreate, onStart and onResume at the Activity your starting. If you want to go back, you usually just press the back button at your device.
You might want to take a look at the Android devGuide and especially at the Activity lifecycle.
I think you may be confused as to what onPause() and onResume() are used for.
onResume() is called when the Activity is about to show it's view to the user. You can use your intent in this method so the main Activity would instantly go to the ClassIndex activity. However, when you leave the ClassIndex activity, onResume() will be called again which will send you back to ClassIndex.
onPause() is called when the Activity is leaving. Whether this is for locking the screen or moving to another Activity. In this case, when you call startActivity in the main Activity, Android will call onPause() in the main activity before entering the new one.
What you may want in this case is to use startActivityForResult() and use it to determine if you're coming back from your ClassIndex activity or not. If not, then start it. If so, then move on to the main activity.
A better solution may-or-may-not be to start the app in ClassIndex and move to your main activity when the user is done with it. This would be the case if you want to move to ClassIndex every time the user enters the app.

startActivity on Android

so i've been trying to get my application to run an Activity via an intent and it works fine, when i then assign the finish(); method, it returns to the activity that called it. The only thing i don't understand is that i'm not sure if the callee Activity is put onPause while the called Activity is in-front. I've tried to setup a toast message in the onPause() method of the callee Acitivty but it won't appear.
I first tried to call the second Activity with startActivity(intentname) and then a finish() method on the first Acitivty, i then tried to use the startActivityForResult() (even though i don't really need to recieve any information from the called Activity) method and closed it with onActivityResult().
I can't find any information about the side-effects that these Activity methods has on a Activity that's calling another. So i'm wondering if anybody could help me out ?
//Thx in advance
According to the documentation for Activity, the onPause() lifecycle method WILL be called when another Activity is put in front of it.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If the called Activity is is semi-transparent, then onStop() will also be called, but if your initial Activity is not visible at all, onStop() will not be called.
It is also of worth to note that when you call finish() on the called Activity, the onResume() will be called on the caller (and onStart(), assuming onStop() was also called)
To quickly answer your question: if activity A starts activity B, then A's onPause method is run. I think there might be an exception if B isn't full screen, but that's only a tentative memory from something I read in the documentation a while ago.
As for why your toast wasn't showing - did you remember to .show() it? I always used to forget to do that. Toasts can also get missed if they're triggered just as the activity is pausing, since its context goes away. There's a much easier way to test it - just use the Log method. For example, Log.d("My app name", "onPause was just triggered"); The purpose of the "My app name" string is to let you filter by it in LogCat. If you don't know how to display LogCat, and assuming you're using Eclipse, see this answer to another question.
got it to work , was a bit confused of the purpose with onResume(), i was suppose to decleare a onActivityResult() in the first Activity so that the second Activity would return to it right after finish()

Categories

Resources