I'm referring to Why use Fragment#setRetainInstance(boolean)?
The reason I ask so is for Activity
to handle rotation, Official Activity Documentation encourages us to let Activity
shut-down and restart during rotation.
android:configChanges Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called. Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.
Any attempt to change this Activity default behavior seems to be bad practice. To avoid Activity from reloading time consuming data structure during restarting, we make make use of onRetainNonConfigurationInstance
and getLastNonConfigurationInstance
. - Official Handling Runtime Changes
However, when comes to handling rotation in Fragment, does Google give us different recommendation? They do not want us to shut down and restart Fragment?
public Object onRetainNonConfigurationInstance ()
This method was deprecated in API level 13. Use the new Fragment API setRetainInstance(boolean) instead; this is also available on older platforms through the Android compatibility package.
- Why does Google encourage us to shut down and restart Activity during rotation, but encourage us to retain Fragment during rotation?
- If
setRetainInstance(true)
is good in handling rotation, why don't Google make it as Fragment's default behavior?