我一直有很多的Fragments
最近并采用两种不同的方法传递对象片段被,但我能看到的唯一区别是,在通过以下FragmentOne采取的方法,你传递的对象必须实现该Serializable
接口( 和一切与此有关 )。
有没有使用一个比其他任何好处?
public class FragmentOne extends Fragment {
public static final String FRAGMENT_BUNDLE_KEY =
"com.example.FragmentOne.FRAGMENT_BUNDLE_KEY";
public static FragmentOne newInstance(SomeObject someObject) {
FragmentOne f = new FragmentOne();
Bundle args = new Bundle();
args.putSerializable(FRAGMENT_BUNDLE_KEY, someObject);
f.setArguments(args);
return f;
}
public SomeObject getSomeObject() {
return (SomeObject) getArguments().getSerializable(FRAGMENT_BUNDLE_KEY);
}
}
和
public class FragmentTwo extends Fragment {
SomeObject mSomeObject;
public static FragmentTwo newInstance(SomeObject someObject) {
FragmentTwo fragment = new FragmentTwo();
fragment.setSomeObject(someObject);
return fragment;
}
public void setSomeObject(SomeObject someObject) {
mSomeObject = someObject;
}
}