Hi I was looking at the following Fragments example on the android site.
嗨,我在Android网站上看下面的片段示例。
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.
我想知道为什么要执行某些方法。
Why for instance, in the detailsFragment
is the following method performed:
例如,为什么在detailsFragment中执行以下方法:
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;
}
Could you not also simply instantiate the DetailsFragment
and use a setter method to set index
instead. Bypassing the whole setArguments
.
您是否也可以简单地实例化DetailsFragment并使用setter方法来设置索引。绕过整个setArguments。
What's the point of using setArguments
in the first place? Could you not just use setters and getters?
首先使用setArguments有什么意义?你能不能只使用二传手和吸气剂?
4 个解决方案
#1
36
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.
您可以使用getter和setter,但是通过传入一个bundle,您不需要编写该代码,因为它已经存在。此外,我相信如果屏幕方向改变,这些参数会自动传入,这也使生活更轻松。
Essentially, setArguments and getArguments is just a design pattern that Google suggests you follow:
从本质上讲,setArguments和getArguments只是Google建议您遵循的设计模式:
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
每个片段都必须有一个空构造函数,因此可以在恢复其活动状态时进行实例化。强烈建议子类没有带参数的其他构造函数,因为在重新实例化片段时不会调用这些构造函数。相反,参数可以由调用者使用setArguments(Bundle)提供,稍后由Fragment使用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.
我认为这包括你的Fragment运行所需的setter。再说一遍 - 没有什么可以强迫你这样做,而且正如你所知道的那样 - 这不是让事情成功的唯一方式。
#2
23
Just to add to Matthew's answer: he quoted correctly that Fragments need to have an empty constructor, so that the framework can re-instantiate them when needed.
只是为了补充Matthew的答案:他正确引用了Fragments需要一个空构造函数,以便框架可以在需要时重新实例化它们。
It is fine to use getters and setters, but as the framework may destroy and re-create your Fragment, you must ensure to not lose those parameters.
使用getter和setter是好的,但是由于框架可能会破坏并重新创建Fragment,因此必须确保不丢失这些参数。
This must be done via Fragment.onSaveInstanceState()
. The saved stated will be passed back to you as the parameter savedInstanceState
in Fragment.onCreate()
, Fragment.onCreateView()
and several other methods.
这必须通过Fragment.onSaveInstanceState()完成。保存的声明将作为Fragment.onCreate(),Fragment.onCreateView()中的参数savedInstanceState和其他几种方法传回给您。
Using Fragment.setArguments()
is (in most cases, I assume) easier, at the framework will automatically preserve the arguments and thus will do most of the work for you.
使用Fragment.setArguments()(在大多数情况下,我假设)更容易,在框架将自动保留参数,从而将为您完成大部分工作。
Setters may be the way to go for parameters you supply to the Fragment initially and which the fragment may adjust itself over time. Dealing with the savedInstanceState alone may be easier in this case than dealing with savedInstanceState and arguments - where you have to make a decision which is the valid parameter.
Setter可能是您最初提供给Fragment的参数以及片段随时间调整的方式。在这种情况下,单独处理savedInstanceState可能比处理savedInstanceState和参数更容易 - 您必须做出决定哪个是有效参数。
#3
5
public void setArguments (Bundle args)
Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation (may be the text in bold was missing from official documentation previously)
提供此片段的构造参数。这只能在片段附加到其活动之前调用;也就是说,你应该在构造片段后立即调用它。这里提供的参数将在片段销毁和创建中保留(可能是以前的官方文档中缺少粗体的文本)
Fragments.setArguments(Bundle args)
Fragments.setArguments(Bundle args)
#4
0
In addition setters can be misused. If updateSomeOtherStuff() will change some view this will crash.
此外,设置者可能被滥用。如果updateSomeOtherStuff()将更改某些视图,则会崩溃。
public class MyFragment extends Fragment {
void setData(someData){
this.someData = someData;
updateSomeOtherStuff()
}
}
If one passing in a bundle it is not possible to misuse the setter and you will always know that this will be set within the lifecycle methods.
如果传入一个bundle,就不可能滥用setter,你将始终知道这将在生命周期方法中设置。
#1
36
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.
您可以使用getter和setter,但是通过传入一个bundle,您不需要编写该代码,因为它已经存在。此外,我相信如果屏幕方向改变,这些参数会自动传入,这也使生活更轻松。
Essentially, setArguments and getArguments is just a design pattern that Google suggests you follow:
从本质上讲,setArguments和getArguments只是Google建议您遵循的设计模式:
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
每个片段都必须有一个空构造函数,因此可以在恢复其活动状态时进行实例化。强烈建议子类没有带参数的其他构造函数,因为在重新实例化片段时不会调用这些构造函数。相反,参数可以由调用者使用setArguments(Bundle)提供,稍后由Fragment使用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.
我认为这包括你的Fragment运行所需的setter。再说一遍 - 没有什么可以强迫你这样做,而且正如你所知道的那样 - 这不是让事情成功的唯一方式。
#2
23
Just to add to Matthew's answer: he quoted correctly that Fragments need to have an empty constructor, so that the framework can re-instantiate them when needed.
只是为了补充Matthew的答案:他正确引用了Fragments需要一个空构造函数,以便框架可以在需要时重新实例化它们。
It is fine to use getters and setters, but as the framework may destroy and re-create your Fragment, you must ensure to not lose those parameters.
使用getter和setter是好的,但是由于框架可能会破坏并重新创建Fragment,因此必须确保不丢失这些参数。
This must be done via Fragment.onSaveInstanceState()
. The saved stated will be passed back to you as the parameter savedInstanceState
in Fragment.onCreate()
, Fragment.onCreateView()
and several other methods.
这必须通过Fragment.onSaveInstanceState()完成。保存的声明将作为Fragment.onCreate(),Fragment.onCreateView()中的参数savedInstanceState和其他几种方法传回给您。
Using Fragment.setArguments()
is (in most cases, I assume) easier, at the framework will automatically preserve the arguments and thus will do most of the work for you.
使用Fragment.setArguments()(在大多数情况下,我假设)更容易,在框架将自动保留参数,从而将为您完成大部分工作。
Setters may be the way to go for parameters you supply to the Fragment initially and which the fragment may adjust itself over time. Dealing with the savedInstanceState alone may be easier in this case than dealing with savedInstanceState and arguments - where you have to make a decision which is the valid parameter.
Setter可能是您最初提供给Fragment的参数以及片段随时间调整的方式。在这种情况下,单独处理savedInstanceState可能比处理savedInstanceState和参数更容易 - 您必须做出决定哪个是有效参数。
#3
5
public void setArguments (Bundle args)
Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation (may be the text in bold was missing from official documentation previously)
提供此片段的构造参数。这只能在片段附加到其活动之前调用;也就是说,你应该在构造片段后立即调用它。这里提供的参数将在片段销毁和创建中保留(可能是以前的官方文档中缺少粗体的文本)
Fragments.setArguments(Bundle args)
Fragments.setArguments(Bundle args)
#4
0
In addition setters can be misused. If updateSomeOtherStuff() will change some view this will crash.
此外,设置者可能被滥用。如果updateSomeOtherStuff()将更改某些视图,则会崩溃。
public class MyFragment extends Fragment {
void setData(someData){
this.someData = someData;
updateSomeOtherStuff()
}
}
If one passing in a bundle it is not possible to misuse the setter and you will always know that this will be set within the lifecycle methods.
如果传入一个bundle,就不可能滥用setter,你将始终知道这将在生命周期方法中设置。