Android's LoaderManager APIs can be used to hook up the new instance of the Activity ( after an orientation change) with the existing Loader (started by the old activity instance before the orientation change). The internal implementation is described here and it works fine except for one use case. It works fine because Android framework is able to detect the orientation change and invokes a specific API to save info in the old activity instance. However, there is one use where things aren't saved,
* User launches the activity and triggers the background operation to launch the Loaders
* User pushes the application to background via Home key
* User changes the orientation in Launcher activity
* User resumes the app from the recent app list
Now, as the application is resumed, Android has to destroy the old instance and recreate a new instance to handle the new orientation. However, as and when this happens, getSupportLoaderManager().getLoader(int) in the new Activity instance isn't going to return the Loader instance and instead would return null. This is because the loader info was never saved when the activity was paused and stopped. Loader info is saved only when orientation changes while the activity is in foreground.
This has an important implication for application developers. As and when this happens, the existing Loader instance would never be hooked up with the new activity instance and would end up break their applications. This should be handled by the Framework but meanwhile, application developers have to handle this use case by themselves,
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("LOADERSTARTED", mLoaderStarted );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
...
if ( savedInstanceState != null ) {
mLoaderStarted = savedInstanceState.getBoolean( "LOADERSTARTED" );
}
if ( mLoaderStarted ) {
getSupportLoaderManager().initLoader( LOADER_ID, null, this);
}
}
The idea is for the activity to keep track if the Loader was started and then hook up the new activity instance with the Loader accordingly. This would handle all cases of orientation change.
* User launches the activity and triggers the background operation to launch the Loaders
* User pushes the application to background via Home key
* User changes the orientation in Launcher activity
* User resumes the app from the recent app list
Now, as the application is resumed, Android has to destroy the old instance and recreate a new instance to handle the new orientation. However, as and when this happens, getSupportLoaderManager().getLoader(int) in the new Activity instance isn't going to return the Loader instance and instead would return null. This is because the loader info was never saved when the activity was paused and stopped. Loader info is saved only when orientation changes while the activity is in foreground.
This has an important implication for application developers. As and when this happens, the existing Loader instance would never be hooked up with the new activity instance and would end up break their applications. This should be handled by the Framework but meanwhile, application developers have to handle this use case by themselves,
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("LOADERSTARTED", mLoaderStarted );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
...
if ( savedInstanceState != null ) {
mLoaderStarted = savedInstanceState.getBoolean( "LOADERSTARTED" );
}
if ( mLoaderStarted ) {
getSupportLoaderManager().initLoader( LOADER_ID, null, this);
}
}
The idea is for the activity to keep track if the Loader was started and then hook up the new activity instance with the Loader accordingly. This would handle all cases of orientation change.
No comments:
Post a Comment