thanks to this answer Android Fragment lifecycle issue (NullPointerException on onActivityResult) I've managed to re-create a scenario when I receive a NPE in my fragment after calling startActivityForResult. So i have
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
break;
being called from my fragment, then my activity receives onPause, onStop and onDestroy, so the fragment that called startActivityForResult
gets an onDestroy as well. After I pick an image, i get a new onCreate on my activity and then i get a public void onActivityResult
on my original fragment that is now destroyed.
My question is since this is potentially (albeit a rare) situation, how would one restore the entire stack of fragments and objects passed to them and what does one do to prevent the original fragment from leaking?
When your code executes following line :
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
Your activity will get onPause()
, onStop()
but not onDestroy()
everytime. If android system is out of memory at that run time then your activity may get onDestroy()
For saving state of fragment and activity and prevent leaking :
Save your state in onSaveInstanceState(Bundle)
In situations where the system needs more memory it may kill paused processes (after
onPause()
) to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In generalonSaveInstanceState(Bundle)
is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)
Restore your state in onRestoreInstanceState(Bundle)
onRestoreInstanceState(Bundle)
method is called afteronStart()
when the activity is being re-initialized from a previously saved state, given here insavedInstanceState
. Most implementations will simply useonCreate(Bundle)
to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen byonSaveInstanceState(Bundle)
.
This may help you implement your requirement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With