简体   繁体   中英

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."

Under what circumstances will 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM