When is it NOT okay to use onRetainNonConfigurationInstance()
Documentation of onRetainNonConfigurationInstance()
says "This function is called purely as an optimization, and you must not rely on it being called."
onRetainNonConfigurationInstance()
not be called?
Some background on what I am trying to achieve: I want to my activity
to be notified of orientation
change reliably (but also want activity to be killed & restarted as usual).
if you want the activity to restart (calling onCreate->onResume) again on orientation change you don't place configChanges="orientation" in the manifest. you can check the orientation of the screen in the onCreate method of your activity which is probably what you need. if you dont want the activity to restart itself but rather just switch orientation then you add the configChanges flag in the manifest for the activity and then only onConfigurationChanged() will be called where you also can get the screen orientation. the second way is good when you have expensive operations running in the onCreate methods (starting threads quering databases etc) and you want to reuse that info/data for portrait and for landscape. even if you want you can change the layout in onConfigurationChanged() the same way its done in on create but you again need to find the references to all the views because their ids are not the same in that case.
In any case if you want to have a reference to something that existed before the orentation change the configChanges way is better for handling the changes but it requires a bit more work if you are changing layouts or something.
精彩评论