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

一世执手 提交于 2020-01-03 15:17:07

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/10316527/dailogfragment-getarguments-setarguments-why-passing-arguments-in-a-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!