0

I have an App that has a single Activity with a Navigation Drawer, which switches between many, many fragments. These Fragments obviously have UIs, so I shouldn't call setRetainInstance(true) on them directly.

For data like Bitmaps which are too large to be stored in a Bundle with onSaveInstanceState(), what is the recommended practice to save/restore their state? The Official Docs recommend that Activities save such data in UI-less Fragments. I also know it's possible to nest Fragments if needed, but is that recommended for cases like this? Should each UI Fragment really have a corresponding data retention fragment class? Or (perhaps even worse), a monolithic data retention class for all UI Fragments to share?

It just seems like there has to be a better way.

automaton
  • 495
  • 6
  • 16

1 Answers1

1

Have you heard about the Android Architecture Components that were recently published by Google? Part of these components is the ViewModel which can hold data and survives configuration changes. Read more about this ViewModel in this post, in the official documentation or in general about the architecture components here.

guglhupf
  • 1,298
  • 10
  • 13
  • To me, this functionally seems like the same thing as the UI-less fragment, except perhaps a little cleaner than mucking with tags and the FragmentManager. But the article does answer the heart of my question with these two statements: `Note that ViewModels can be easily used with both Fragments and Activities... In general, you’ll make a ViewModel class for each screen in your app.` Much appreciated @guglhupf – automaton Dec 12 '17 at 21:05