3

Probably it is a well-known topic, but I didn't find any "universal" solution.

The main problems are two:

  • When a fragment is put to backstack, onSavedInstanceState is never called
  • Why can't you instantiate fragment's views in onCreate?

And at this point my question is: since the fragment lifecycle goes through onCreateView every resume, and since we can't instantiate views in onCreate; why do we have to re-create views every time the fragment is shown? There must be a way as in the Activity with onCreate.

We might avoid by adding a class-scoped boolean and check if it has a value, but it means adding an if and this can't be the best solution. Another solution might be this one, but it is also a workaround and it also has some limitations.

Is there a built-in (or a standard) solution that solve this problem?

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66

1 Answers1

1

I'm unclear on the meaning of the title of your question - but I can answer these three points:

When a fragment is put to backstack, onSavedInstanceState is never called

onSavedInstanceState is called when saving state - just adding to the backstack is not causing that to happen - it happens when pausing the fragment if there is some UI showing. Actually a good answer addressing this problem exists here (link)

Why can't you instantiate fragment's views in onCreate?

A fragment is resumed from the onPostResume method of an Activity, only at this point can you safely touch the views of a fragment. (Inside the fragment you can use onViewsCreated)

why do we have to re-create views every time the fragment is shown

This can be avoided by retaining your fragment instances (although there are drawbacks). You can retain by using:

setRetainInstance(true)
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • Thanks for the answer! I have just a couple of questions: afaik, using onSavedInstanceState of the activity has some problems when using a ViewPager, is it right? the second one is that Retain is a good option but it has, as you pointed out, a couple of problems (first of all, the performance one). But if those are the only options, I don't really have many possibilities :) – Pier Giorgio Misley Jan 19 '18 at 14:53
  • Well, what is your aim? Just to know if it is the first time a particular fragment is shown? For that your `class-scoped boolean` is definitely the cleanest approach. If not, instead of getting a complete instance state from the fragment your activity could store the value from the fragment inside it's own state (including restoring the variable (not the views) when restored – Nick Cardoso Jan 19 '18 at 14:58
  • Ok, thanks! yeah I would like to know if it is the first time, so I can avoid calling `view.findViewById(...)` every time I pop the fragment. – Pier Giorgio Misley Jan 19 '18 at 15:03