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. 

 

Androidgears

Android Adventures With Android Studio

Contents

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Lifecycle and State
  5. Basic Controls And Events
  6. Spinners
  7. Pickers
  8. UI Graphics A Deep Dive
  9. ListView And Adapters
  10. Introducing Fragments
  11. Fragments and XML
  12. Fragment And Activity Working Together
  13. Managing Fragments
  14. Custom dialogs using DialogFragment
  15. Dialog Classes In DialogFragment
  16. A NumberPicker DialogFragment Project

 See Also:

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 ( Friday, 17 October 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.