: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 ..??
发布于 2012-02-21 22:01:20
在onCreate() (代码中的myBundle)中传递的包就是所谓的savedInstanceState。您可以将片段中的一些数据(“状态”)保存在方法onSaveInstanceState()中的包中,稍后该包将在onCreate()和其他一些方法中可用。
getArguments()方法返回的包是从片段的调用者传递过来的包。这个包是通过setArguments()方法提供的。
https://stackoverflow.com/questions/9378567
复制相似问题