activity_revival_and_the_case_of_the_rotating_device
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext 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] kalaschnikow | activity_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:// | ||
| + | |||
| + | ===== What can be done to handle configuration change ===== | ||
| + | |||
| + | |||
| + | Options: | ||
| + | |||
| + | 1. Save/ | ||
| + | |||
| + | 1.1 onSaveInstanceState() + onRestoreInstanceState() | ||
| + | 1.2 onSaveInstanceState() + restore in onCreate() from Bundle | ||
| + | |||
| + | It doesn' | ||
| + | |||
| + | |||
| + | 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 | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== 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:// | ||
| + | |||
