Android Adventures - Lifecycle And State
Written by Mike James   
Monday, 10 June 2013
Article Index
Android Adventures - Lifecycle And State
The Bundle
Advanced State Management


Advanced State Management

For completeness it is worth noting that there are many more approaches to maintaining state. Later you will need to discover how to store lots of user data locally for longer term persistence and this is often enough to implement state management though a configuration change. 

You can also opt to retain an object of your choosing using the onRetainNonConfigurationInstance(). All you have to do is override this function so that it returns the object you want to keep. The system will then store this object for you and return it to you when you ask for it using getLastNonConfigurationInstance() which returns the object that was last saved by the system. 

The object can be as complex as you like but don't try to save a system object such as an Activity or a View. This just results in memory leaks.

The ultimate in making sure that things happen as you want it to handle the configuration change yourself. 

You can do this by making a change to the manifest. If you do this then it is up to you to make the changes needed when the onConfigurationChanged even occurs.  You could for example opt to animate buttons and other UI objects into new positions or just ignore the need to reconfigure altogether. 


Android Adventures With Android Studio


  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Basic Controls And Events
  5. Layout Containers
  6. Lifecycle and State
  7. Spinners
  8. Pickers
  9. UI Graphics A Deep Dive
  10. ListView And Adapters

  11. Introducing Fragments
  12. Fragments and XML
  13. Fragment And Activity Working Together
  14. Managing Fragments
  15. Custom dialogs using DialogFragment
  16. Dialog Classes In DialogFragment
  17. A NumberPicker DialogFragment Project
  18. ViewPager

If you are interested in creating custom template also see:

Custom Projects In Android Studio



Coming Next

In the next installment we'll discover how to use the basic UI components and how to handle events in general.

Meanwhile if you have any questions on what we've covered so far please let me know using the comments.


You can download the code for this program and for the simple blank activity template from the CodeBin (note you have to register first).


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.


blog comments powered by Disqus

Last Updated ( Thursday, 20 November 2014 )

Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.