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)));
}
}
有时我收到:假,真,假,假有时:假,假,假,假 无论如何.. ??
最佳答案
在 onCreate()
中传递的 Bundle(您代码中的 myBundle)称为 savedInstanceState。您可以在方法 onSaveInstanceState()
中的包中的 fragment 中保存一些数据(“状态”)稍后这个包将在 onCreate()
和其他一些方法中可用。
由 getArguments()
返回的 bundle 方法是从 fragment 的调用者传递的包。此包通过 setArguments()
方法提供。
关于Android 和 bundle ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9378567/