Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment being destroyed yet receiving onActivityResult

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?

like image 637
Dmitry Golomidov Avatar asked Apr 06 '15 04:04

Dmitry Golomidov


1 Answers

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 general onSaveInstanceState(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 after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(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 by onSaveInstanceState(Bundle).

This may help you implement your requirement

like image 68
Kushal Avatar answered Oct 05 '22 09:10

Kushal