第二次加载视图时出现问题。第一次没有问题,我的片段正确加载,但第二次有我不知道默认视图或空。这是我的基本代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contact, container, false);
Context context = getActivity();
// TODO Auto-generated method stub
List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>();
fragments.add(android.support.v4.app.Fragment.instantiate(context, Fragment1.class.getName()));
fragments.add(android.support.v4.app.Fragment.instantiate(context, Fragment2.class.getName()));
fragments.add(android.support.v4.app.Fragment.instantiate(context, Fragment3.class.getName()));
mPagerAdapter =new PagerAdapter(getActivity().getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) rootView.findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
return rootView;
来自MainActivity:
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}