我正在使用Fragments其中一个创建一个应用程序,我创建了一个非默认构造函数并得到了这个警告:
Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么这不是一个好主意?
你能否建议我如何做到这一点:
public static class MenuFragment extends ListFragment {
public ListView listView1;
Categories category;
//this is my "non-default" constructor
public MenuFragment(Categories category){
this.category = category;
}....
Run Code Online (Sandbox Code Playgroud)
不使用非默认构造函数?
我有2个片段:(1)Frag1(2)Frag2.
bundl = new Bundle();
bundl.putStringArrayList("elist", eList);
Frag2 dv = new Frag2();
dv.setArguments(bundl);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.the_fragg,dv);
ft.show(getFragmentManager().findFragmentById(R.id.the_fragg));
ft.addToBackStack(null);
ft.commit();
Run Code Online (Sandbox Code Playgroud)
如何在Frag2中获取此数据?
嗨,我在Android网站上看下面的片段示例.
http://developer.android.com/guide/components/fragments.html#Example
我想知道为什么要执行某些方法.
例如,为什么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;
}
Run Code Online (Sandbox Code Playgroud)
您是否也可以简单地实例化DetailsFragment并使用setter方法来设置index.绕过整个setArguments.
setArguments首先使用的重点是什么?你能不能只使用二传手和吸气剂?