0

Whats the difference between initialising a fragment using: -

  • a static newInstance(String a, int b) method and Bundle arguments.
  • vs. a multi-param constructor MyFragment(String a, int b), provided I also add a no argument constructor MyFragment() ?

And if its got to do with how the system initialises Fragments, what's the purpose of onSavedInstanceState(Bundle bundle) vs MyFragment.newInstance(String a, int b)?

aprofromindia
  • 1,111
  • 1
  • 13
  • 27

2 Answers2

0

When the OS needs to destroy and recreate your fragment it creates a new instance of it using the empty constructor.

The bundle you create and set in newInstance() is saved and restored for you by the OS and contains only the initial arguments for the fragment.

The bundle you receive in onSavedInstanceState(Bundle) is a whole different one which you should populate with fields representing the state of your fragment (as opposed to initial arguments in the former bundle). You receive the same bundle in the recreated fragment instance and you can extract the state from it when onActivityCreated(Bundle) is called.

Hanan Rofe Haim
  • 870
  • 6
  • 11
  • The arguments set in the overloaded constructor are saved as well – OneCricketeer Feb 14 '17 at 23:23
  • saved by whom?. – Hanan Rofe Haim Feb 15 '17 at 00:24
  • You said that yourself. "The bundle you create and set in newInstance() is saved and restored for you by the OS". NewInstance saves nothing that the overloaded constructor does not – OneCricketeer Feb 15 '17 at 04:56
  • "NewInstance saves nothing that the overloaded constructor does not" That's wrong, the OS knows how to save your arguments since they're stored in a bundle and that bundle is set on the fragment. Simply using a constructor with arguments would mean android will have no way of saving and restoring your arguments automatically, it'll be up to you to manage it manually. – Hanan Rofe Haim Feb 15 '17 at 10:36
  • I think you misunderstood. This is what I mean. http://stackoverflow.com/a/25994861/2308683 – OneCricketeer Feb 15 '17 at 16:09
  • OK now I get what you're saying, yes it'll work, but it's still a bad practice and can lead to problems in the future. For example, suppose some other developer extends your Fragment, then the same constructor would be defined but a call to a super(arg1,arg2) constructor will be a mess since you'll setBundle() twice, once in the original constructor and once in the extending fragment. – Hanan Rofe Haim Feb 15 '17 at 19:01
0

provided I also add a no argument constructor

In that case. No difference.

As far as onSavedInstanceState goes, that is further into the lifecycle of the Fragment. If you ever updated the arguments that you received from setArguments , then you would need to implement a method to save them in order to persist state, and then you would use onSavedInstanceState and the Bundle provided by onCreateView instead of or in addition to getArguments

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245