Reputation: 183
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?
Upvotes: 1
Views: 1233
Reputation: 136
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
Upvotes: 1
Reputation: 647
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.
Upvotes: 2
Reputation: 674
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
Upvotes: 0