Skip to content Skip to sidebar Skip to footer

Why Oncreate Method Called After Startactivityforresult?

In my application i want to select image from Gallery and set the image in ListView. In my ListViewI have 16 rows. So when ever item click inListViewopen the gallery and set the im

Solution 1:

before the onactivityresult(). But after oncreate() method again startactivity result called

when Activity is sent to background (when other activity becomes on top of it, or when it is sent by the home button to background) its instance is kept alive as long as the system is not under memory pressure. when the system doesn't have enough memory to do whatever it's currently doing in foreground, it usually will re-claim memory by stopping and releasing from memory background activities.

in that case - the system provide you with the Activity.onSaveInstanceState callback which will be invoked from the activity that's going to be killed to provide you a chance to save any state needed to be saved before it's being killed.

when your activity will return to foreground - it will be re-created (that's why onCreate() called again), with savedInstanceState parameter that will not be null.

the savedInstanceState will hold bundle with all the extras you provided in the onSavedInstanceState() callback.

this is very important to understand.

for better understanding, I advise you to read seriously - http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Solution 2:

May be dialog with selection chooser is too huge for the system and it reduces memory by caching your application. Check is method onSaveInstanceState called before onCreate. If yes, then you can save needed data in bundle and load it in onCreate method

Post a Comment for "Why Oncreate Method Called After Startactivityforresult?"