User Tools

Site Tools


activity_revival_and_the_case_of_the_rotating_device

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
activity_revival_and_the_case_of_the_rotating_device [2016/11/07 14:18] – [What can be done to handle configuration change] kalaschnikowactivity_revival_and_the_case_of_the_rotating_device [2018/04/23 15:57] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Activity Revival and the case of the Rotating Device ======
 +by Joanna Smith
 +
 +[[https://medium.com/google-developers/activity-revival-and-the-case-of-the-rotating-device-167e34f9a30d#.b4qdgllj6|Original]]
 +
 +===== What can be done to handle configuration change =====
 +
 +
 +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.
 +
 +<code java>
 +@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
 +  }
 +} 
 +</code>
 +
 +
 +===== Background work =====
 +
 +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 [[https://plus.google.com/+AndroidDevelopers/posts/cEuiekwpTjU|protip]] about that, as well!)
 +