什么是setArguments的地步? [英] What's the point of setArguments?

查看:168
本文介绍了什么是setArguments的地步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我一直在寻找对Android网站下面的片段的例子。

Hi I was looking at the following Fragments example on the android site.

<一个href="http://developer.android.com/guide/components/fragments.html#Example">http://developer.android.com/guide/components/fragments.html#Example

我想知道为什么某些方法进行。

I would like to know why certain methods are performed.

为什么,例如,在 detailsFragment 是以下方法进行:

Why for instance, in the detailsFragment is the following method performed:

public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);

    return f;
}

你能不能也简单地初始化 DetailsFragment 并使用setter方法​​来设置首页代替。绕过整个 setArguments

Could you not also simply instantiate the DetailsFragment and use a setter method to set index instead. Bypassing the whole setArguments.

什么是摆在首位使用 setArguments 的地步?难道你不只是使用getter和setter方法​​?

What's the point of using setArguments in the first place? Could you not just use setters and getters?

推荐答案

您可以使用getter和setter方法​​,而是通过传递一个包你不需要编写code,因为它已经存在。另外,我认为,这些参数将自动传递再次,如果屏幕方向的变化,这也使生活更轻松。

You can use getters and setters, but by passing in a bundle you don't need to write that code, since it's already there. Also, I believe that these arguments are automatically passed in again if the screen orientation changes, which also makes life easier.

从本质上讲,setArguments和getArguments只是一种设计模式,谷歌建议您遵循:

Essentially, setArguments and getArguments is just a design pattern that Google suggests you follow:

每一个片段都必须有一个空的构造,所以可以   当恢复其活性的状态实例化。强烈   建议子类不具有其他构造与   参数,因为这些构造不会被调用时,   片段重新实例;相反,参数可以被供给   主叫方与setArguments(捆绑),后来被片段检索   与getArguments()。    http://developer.android.com/reference/android/app/Fragment.html

Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments(). http://developer.android.com/reference/android/app/Fragment.html

我采取包括需要哪些为您片段操作以及制定者。话又说回来 - 没有什么强迫你做这种方式,正如你知道 - 这不是事情可以作出的唯一方法

I take that to include setters which are needed for your Fragment to operate as well. Then again - there's nothing forcing you to do it this way, and as you know - it's not the only way things could be made to work.

这篇关于什么是setArguments的地步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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