我最近开始在阅读后的所有片段中使用newInstance()
方法,这是正确的方法。然而,我感到不安的是它没有按照我的预期运作。我的问题是,在getArguments()
方法中调用onCreate(Bundle savedInstanceState)
后,setArguments()
中的newInstance()
为空。
我的片段:
public static CreateCoverFragment newInstance(CreateCoverModel createCoverModel) {
CreateCoverFragment createCoverFragment = new CreateCoverFragment();
Bundle args = new Bundle();
args.putParcelable(NvpKeys.CREATE_COVER_MODEL, createCoverModel);
createCoverFragment.setArguments(args);
return createCoverFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
mModel = args.getParcelable(NvpKeys.CREATE_COVER_MODEL);
} else {
Log.e(TAG, "getArguments() is null...WTF!");
}
}
在我的FragmentPagerAdapter中,我回来了:
CreateCoverFragment.newInstance(mCreateItineraryModel.getCreateCoverModel());
我的日志是:
03-01 16:15:50.204 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 0)
03-01 16:15:50.204 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate()
添加新页面并在notifyDataSetChanged()
中调用PagerAdapter
后,再次创建CreateCoverFragment
,这就是问题所在。
03-01 16:15:54.934 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 1)
03-01 16:15:54.934 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate()
03-01 16:15:54.934 30043-30043/com.blunka.harry E/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ getArguments() is null...WTF!
编辑:
我在CreateCoverFragment的生命周期方法中添加了一些日志记录,而且更加困惑。
9:23:31.552 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 0)
03-01 19:23:31.552 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ newInstance()
03-01 19:23:31.562 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ CreateCoverFragment
03-01 19:23:31.562 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onAttach()
03-01 19:23:31.562 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate()
将页面添加到PagerAdapter ...
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 1)
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ CreateCoverFragment
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onAttach()
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate()
03-01 19:23:32.402 4129-4129/com.blunka.harry E/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ getArguments() is null...WTF!
默认构造函数,onAttach()和onCreate()方法如何以及为什么第二次被调用而没有首先调用newInstance(),onDestroy()或onDetach()?
编辑:
我是个傻瓜。我在新片段中使用了CreateCoverFragment的默认构造函数。
答案 0 :(得分:1)
getArguments()获取首次实例化片段时在包中提供的任何参数 - 在您设置以下行的情况下:
CreateCoverFragment createCoverFragment = new CreateCoverFragment(); // No bundle set at this point
根据您的代码,当调用onCreate时,片段没有提供包。将提取bundle参数的代码放入onCreateView()方法中,它应该可以正常工作。