Fragment And Activity Working Together
Fragment And Activity Working Together
Written by Mike James   
Thursday, 27 March 2014
Article Index
Fragment And Activity Working Together
Using Bundles
Persisting State
An Interface Callback
Persisting Fragments

Persistent Fragments

Making a Fragment persistent, so that it behaves as it would if the system didn't destroy and automatically recreate it, is the hardest part of working with and understanding Fragments. Once you have mastered this you are well on the way to using them effectively. 

However, there is a trick that can make a Fragment much more persistent than its normally is and some use this to avoid all of the work we have just described.

The RetainInstance property can be used to ask the system not to destroy the Fragment, even when the Activity is destroyed for a configuration change. instead all that happens is that the Fragment is detached from the Activity and left in memory. When the Activity is recreated the Fragment is reAttached. 

This means that the entire state of the Fragment is preserved, only the onAttach and onDetach events occur and the onCreateView is called to restore the Activity's View state. 

You might think that you can use this to avoid some of the work in retaining state manually, but you need to know that that in some cases the Fragment will still be destroyed if the Activity is removed because of low system resources or if the user presses the back button. So you still have to deal with some persistence issues yourself. 

The bottom line is:

don't use setRetainInstance(true) in an attempt to avoid handling Fragment persistence.

Its real value is in implementing more sophisticated behavior when you use a Fragment more like a background worker or when you need to use a Fragment to communicate state to other Activities. 


Now you know how a Fragment's state can be persisted, how it can be initialized and how it can communicate with its Activity.

The next step is to find out how multiple Fragments can work together to produce a user interface that can accommodate a range of different screen sizes in a way that doesn't make any of them look like an afterthought.  


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. UI Graphics A Deep Dive
  7. Menus & The Action Bar
  8. Menus, Context & Popup
  9. Resources
  10. Graphics - Coming Soon
  11. Lifecycle and State
  12. Spinners
  13. Pickers
  14. ListView And Adapters

  15. Introducing Fragments
  16. Fragments and XML
  17. Fragment And Activity Working Together
  18. Managing Fragments
  19. Custom dialogs using DialogFragment
  20. Dialog Classes In DialogFragment
  21. A NumberPicker DialogFragment Project
  22. ViewPager

If you are interested in creating custom template also see:

Custom Projects In Android Studio




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.