DailogFragment - getArguments / setArguments - 为什么传递参数的包? [英] DailogFragment - getArguments/setArguments - why passing arguments in a bundle?

查看:253
本文介绍了DailogFragment - getArguments / setArguments - 为什么传递参数的包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在官方例子的http://developer.android.com/reference/android/app/DialogFragment.html#BasicDialog片段正与使用静态工厂方法的一个包装以捆绑参数和调用无参数的构造函数传递ARGS与setArguments(包)创造的 - 所以我的问题是 - 为什么不干脆让公共构造与这些争论?什么是使用getArguments / setArguments片段的方法的原因 - 也许是对话不能保证每次都重新创建,但重复使用?如果这样的话,当它是怎么回事?
先谢谢了。

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.

推荐答案

强制执行无参数,默认的构造模式允许系统动态必要时重新创建片段。从文档:

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!

这篇关于DailogFragment - getArguments / setArguments - 为什么传递参数的包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆