有什么区别:myBundle& getArguments()返回的那个
@Override
public void onCreate(Bundle myBundle) { //on create() belonging to a Fragment
super.onCreate(myBundle);
// So myBundle vs getArguments()
}
从我的简单测试中,它们不是同一个对象,用以下方法测试:
private void compareThem(Bundle myBundle, Bundle arguments) {
Log.d("---myBundle==null: ", " " + (myBundle==null));
Log.d("---arguments==null: ", " " + (arguments==null));
if(myBundle!=null && arguments!=null) {
Log.d("---myBundle==arguments: ", " " + (myBundle==arguments));
Log.d("---myBundle.equals(arguments): ", " " + (myBundle.equals(arguments)));
}
}
有时我会收到:false,true,false,false,有时:false,false,false,false 无论如何.. ??
答案 0 :(得分:1)
在onCreate()
中传递的Bundle(代码中的myBundle)就是所谓的savedInstanceState。您可以在方法onSaveInstanceState()
中从包中的片段保存一些数据(“状态”),稍后此包将在onCreate()
和其他一些方法中提供。
由getArguments()
方法返回的Bundle是从片段的调用者传递的bundle。此捆绑包是通过setArguments()
方法提供的。