4

Is that compulsory to set data using bundle and set argument? What is wrong Here?

MyFragment frag = new MyFragment ()
frag.setData(mSchoolData);

//add to back stack stuff.
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
arons
  • 43
  • 4

1 Answers1

6

by using the setArgs() you will ensure that this Fragment can be recreated due to lifecycle event...while by passing arguments with your own setters it may not work properly under certain circustances. That is why it is absolutely recommended to either uset Args OR use Intent extras, these will always be automatically provided by the system if the fragment gets recreated.

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • What if I declare the variable as Static? – Skynet Feb 09 '15 at 11:28
  • that would work also..but it is usually not a recommended practice, although it might fit well for some specific solutions. – Alécio Carvalho Feb 09 '15 at 11:29
  • My question was out of curiosity, I would add that using Static variables is discouraged because they are global and it gets very very nasty to manage Static variables if you are depending a lot on them. – Skynet Feb 09 '15 at 11:30
  • @Alécio Carvalho you mean there is best practice .. but it is not wrong right? bcoz Im supporting only portrait orientation and every time I supply data from server – arons Feb 09 '15 at 11:41
  • I wouldn't do it, i only use static variables like that in special circustances, so prefer other solutions over it, use them as last resort and with CAUTION. For instance, by using static variables, you have to ensure that it will be properly unset, or it might lead to unexpected memory leaks. – Alécio Carvalho Feb 09 '15 at 14:29
  • Using setters is a good option if the fragment is retained (setRetained(true) has been called) because the fragment's instance is not destroyed on orientation changes. – Thibaut D. Aug 11 '16 at 07:51