我已经看到了在应用程序中实例化一个新Fragment的两个通用实践:
Fragment newFragment = new MyFragment();
and
Fragment newFragment = MyFragment.newInstance();
第二个选项使用静态方法newInstance(),通常包含以下方法。
public static Fragment newInstance()
{
MyFragment myFragment = new MyFragment();
return myFragment;
}
起初,我认为主要的好处是,我可以重载newInstance()方法,以便在创建新的Fragment实例时提供灵活性——但我也可以通过为Fragment创建重载构造函数来实现这一点。
我错过什么了吗?
一种方法相对于另一种方法有什么好处?还是说这只是一个很好的练习?
setArguments()没有用。这只会带来混乱。
public class MyFragment extends Fragment {
public String mTitle;
public String mInitialTitle;
public static MyFragment newInstance(String param1) {
MyFragment f = new MyFragment();
f.mInitialTitle = param1;
f.mTitle = param1;
return f;
}
@Override
public void onSaveInstanceState(Bundle state) {
state.putString("mInitialTitle", mInitialTitle);
state.putString("mTitle", mTitle);
super.onSaveInstanceState(state);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
if (state != null) {
mInitialTitle = state.getString("mInitialTitle");
mTitle = state.getString("mTitle");
}
...
}
}
setArguments()没有用。这只会带来混乱。
public class MyFragment extends Fragment {
public String mTitle;
public String mInitialTitle;
public static MyFragment newInstance(String param1) {
MyFragment f = new MyFragment();
f.mInitialTitle = param1;
f.mTitle = param1;
return f;
}
@Override
public void onSaveInstanceState(Bundle state) {
state.putString("mInitialTitle", mInitialTitle);
state.putString("mTitle", mTitle);
super.onSaveInstanceState(state);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
if (state != null) {
mInitialTitle = state.getString("mInitialTitle");
mTitle = state.getString("mTitle");
}
...
}
}
由于关于最佳实践的问题,我想补充一点,在使用一些REST web服务时,使用混合方法来创建片段通常是一个好主意
我们不能传递复杂的对象,例如一些用户模型,以防显示用户片段
但我们能做的是,签入onCreate那个用户!=null,如果不是-那么把他从数据层,否则-使用现有。
通过这种方式,我们获得了在Android和snappiness的情况下通过userId重新创建片段的能力,以及通过持有对象本身或仅它的id来创建片段的能力
是这样的:
public class UserFragment extends Fragment {
public final static String USER_ID="user_id";
private User user;
private long userId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userId = getArguments().getLong(USER_ID);
if(user==null){
//
// Recreating here user from user id(i.e requesting from your data model,
// which could be services, direct request to rest, or data layer sitting
// on application model
//
user = bringUser();
}
}
public static UserFragment newInstance(User user, long user_id){
UserFragment userFragment = new UserFragment();
Bundle args = new Bundle();
args.putLong(USER_ID,user_id);
if(user!=null){
userFragment.user=user;
}
userFragment.setArguments(args);
return userFragment;
}
public static UserFragment newInstance(long user_id){
return newInstance(null,user_id);
}
public static UserFragment newInstance(User user){
return newInstance(user,user.id);
}
}