1

I have implemented fragment with UI and AsyncTask. For headless Fragment it's recommended use Fragment in combination with the setRetainInstance() method. My question is how to save data on orientation change in this case if Fragment has UI and background process. Thank you for response.

Martin
  • 2,146
  • 4
  • 21
  • 32
  • http://stackoverflow.com/questions/11182180/understanding-fragments-setretaininstanceboolean Think this will give you what you need. – user1105748 Oct 24 '13 at 10:46

1 Answers1

2

When using setRetainInstance(true) the following methods will not be called during orientationChange.

  • onCreate (will only be called when fragment get created)
  • onDestroy (will only be called when activity gets destroyed (e.g. home button etc.)

The other lifecycle will be called e.g.:

  • onCreateView
  • onResume
  • ...

If you want to retain an object, create it in onCreate and handle logic in onDestroy to handle the destroying of the underlying activity. That object will be untouched when orienation occurred. No need to bundle it up or to persist it somewhere locally.

Just a note regarding the title: you don't retain the views themselfs, these should be destroyed and recreated when orientation change occurs. But the object that indicate the state of a view can be retained.

Tobrun
  • 18,291
  • 10
  • 66
  • 81