Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DailogFragment - getArguments/setArguments - why passing arguments in a bundle?

In the official example http://developer.android.com/reference/android/app/DialogFragment.html#BasicDialog the fragment is being created with use of static factory method that wraps arguments in a Bundle and calls no-args constructor passing args with setArguments(bundle)- so my question is - why not simply make public constructor with these arguments? What is the reason for using getArguments/setArguments fragment's methods - is maybe Dialog not guaranteed to be recreated each time, but reused? if so then when it is happening? Thanks in advance.

like image 633
Tomasz Gawel Avatar asked Apr 25 '12 13:04

Tomasz Gawel


1 Answers

Enforcing a no-arguments, default constructor pattern allows the system to re-create the fragment dynamically when necessary. From the docs:

All subclasses of Fragment must include a public empty constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the empty constructor is not available, a runtime exception will occur in some cases during state restore.

"will often" and "in some cases" leaves it vague. But short of satisfying your curiosity ... arguments it is!

like image 84
Joel Skrepnek Avatar answered Oct 30 '22 17:10

Joel Skrepnek