by Joanna Smith
Options:
1. Save/restore instance state with
1.1 onSaveInstanceState() + onRestoreInstanceState() 1.2 onSaveInstanceState() + restore in onCreate() from Bundle
It doesn't really matter which option is chosen. Both are nearby equal.
2. Handle configuration change in onConfigurationChanged()
It is the option with more control, where the developer can make different things for different configuration changes.
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // Change things } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ // Change other things } }
The system will not destroy AsyncTasks when the Activity is restarted, because AsyncTask is tied to the Activity.
The fix for this problem can be to use AsyncTask in a retained fragment. But the best solution is to use Loaders.
And if you aren’t loading data, but making a network connection, then you should be using JobScheduler for smarter network access and background work. (There’s a protip about that, as well!)