我有一个问题,在我的Activity中我有一个NavigationDrawer,我用来调用3个片段。 我已经为片段激活实现了onSaveInstanceState / onRestoreInstanceState,所有数据都被正确保存,但是当我旋转设备时,savedInstanceState(在onCreate内)不为null而NavigationDrawer不调用片段。
现在我已经打破了思考解决方案......
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dualpane);
if(savedInstanceState == null)
instantiateFragments(null);//init
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (null != toolbar) {
setSupportActionBar(toolbar);
//set the back arrow in the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//Create the AccountHeader
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withSavedInstance(savedInstanceState)
.build();
Fragment_1 = new PrimaryDrawerItem()
.withName("Fragment 1")
.withIdentifier(1);
Fragment_2 = new PrimaryDrawerItem()
.withName("Fragment 2")
.withIdentifier(2);
Fragment_3 = new PrimaryDrawerItem()
.withName("Fragment 3")
.withIdentifier(3);
builder = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withTranslucentStatusBar(false)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(
Fragment_1,
Fragment_2,
Fragment_3
) // add the items we want to use with our Drawer
// .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
// @Override
// public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// // do something with the clicked item :D
// if (drawerItem != null && drawerItem instanceof Nameable) {
// String name = ((Nameable) drawerItem).getName().getText(CustomListActivity.this);
// getSupportActionBar().setTitle(name);
// FragmentManager mFragmentManager = getSupportFragmentManager();
// FragmentTransaction transaction = mFragmentManager.beginTransaction();
// Log.d(TAG, "onCreate: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));
// if (savedInstanceState == null) {
// switch (position) {
// case 1:
// transaction.replace(R.id.fragment_container, Fragment1.newInstance(), FRAGMENT_TAG1).commit();
// break;
// case 2:
// transaction.replace(R.id.fragment_container, Fragment2.newInstance(), FRAGMENT_TAG2).commit();
// break;
// case 3:
// transaction.replace(R.id.fragment_container, Fragment3.newInstance("Terzo"), FRAGMENT_TAG3).commit();
// break;
// }
// }else{
// instantiateFragments(savedInstanceState);
// }
// /* FragmentManager mFragmentManager = getSupportFragmentManager();
// Fragment fragment = mFragmentManager.findFragmentById(R.id.fragment_container);
// fragment1 = (Fragment1) mFragmentManager.findFragmentByTag(FRAGMENT_TAG1);
// fragment2 = (Fragment2) mFragmentManager.findFragmentByTag(FRAGMENT_TAG2);
// fragment3 = (Fragment3) mFragmentManager.findFragmentByTag(FRAGMENT_TAG3);
// if (savedInstanceState == null) {
//
// // List<Fragment> lista = mFragmentManager.getFragments();
// if (fragment == null) {
// mFragmentManager.beginTransaction().add(R.id.fragment_container, Fragment3.newInstance("Terzo"), FRAGMENT_TAG3).commit();
// mFragmentManager.beginTransaction().add(R.id.fragment_container, Fragment2.newInstance(), FRAGMENT_TAG2).commit();
// mFragmentManager.beginTransaction().add(R.id.fragment_container, Fragment1.newInstance(), FRAGMENT_TAG1).commit();
//
// } else {
// //mFragmentManager.beginTransaction().remove(fragment).commit();
// mFragmentManager.beginTransaction().detach(fragment).commit();
//
// switch (position) {
// case 1:
// mFragmentManager.beginTransaction().attach(fragment1).commit();
// break;
// case 2:
// mFragmentManager.beginTransaction().attach(fragment2).commit();
// break;
// case 3:
// //bSavePage = new Bundle();
// //bSavePage.putString("section_name", "Quarto");
// //fragment3.setArguments(bSavePage);
// Bundle args = new Bundle();
// args.putString("someString", "Quarto");
// // Put any other arguments
// fragment3.setArguments(args);
// Fragment.instantiate(getApplicationContext(), Fragment3.class.getName(), args);
// mFragmentManager.beginTransaction().attach(fragment3).commit();
// break;
// }
// }
// } else {
// // probabilmente e' accaduta una rotazione
// mFragmentManager.beginTransaction().detach(fragment).commit();
// switch (position) {
// case 1:
// mFragmentManager.beginTransaction().attach(fragment1).commit();
// break;
// case 2:
// mFragmentManager.beginTransaction().attach(fragment2).commit();
// break;
// case 3:
// Bundle args = new Bundle();
// args.putString("someString", "Quarto");
// // Put any other arguments
// fragment3.setArguments(args);
// Fragment.instantiate(getApplicationContext(), Fragment3.class.getName(), args);
// mFragmentManager.beginTransaction().attach(fragment3).commit();
// break;
// }
// }*/
// getSupportActionBar().setTitle(name);
// }
// return false;
// }
// }
//
// )
.withOnDrawerListener(new Drawer.OnDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
KeyboardUtil.hideKeyboard(CustomListActivity.this);
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
}
)
.withFireOnInitialOnClick(true)
.withSavedInstance(savedInstanceState);
result = builder.build();
@Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState);
//add the values which need to be saved from the accountHeader to the bundle
outState = headerResult.saveInstanceState(outState);
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState");
FragmentManager mFragmentManager = getSupportFragmentManager();
mMyFragment = mFragmentManager.findFragmentById(R.id.fragment_container);
mFragmentManager.putFragment(outState, "SaveFrag", mMyFragment);
outState.putString("TAG", mMyFragment.getTag());
}
@Override
protected void onRestoreInstanceState(Bundle inState) {
super.onRestoreInstanceState(inState);
Log.d(TAG, "onRestoreInstanceState");
instantiateFragments(inState);
}
private void instantiateFragments(Bundle inState) {
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = mFragmentManager.beginTransaction();
if (inState != null) {
mMyFragment = (Fragment) mFragmentManager.getFragment(inState, "SaveFrag");
transaction.replace(R.id.fragment_container, mMyFragment, inState.getString("TAG"));
} /*else {
// inizializzazione
transaction.add(R.id.fragment_container, Fragment1.newInstance(), FRAGMENT_TAG1);
transaction.commit();
}*/
}
编辑** 我已经在我的Activity中实现了onPostCreate,并将内部的代码与OnDrawerItemClickListener一起移动,现在可以工作了!
@Override
protected void onPostCreate( final Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Log.d(TAG, "onPostCreate");
builder.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem != null && drawerItem instanceof Nameable) {
String name = ((Nameable) drawerItem).getName().getText(CustomListActivity.this);
getSupportActionBar().setTitle(name);
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = mFragmentManager.beginTransaction();
Log.d(TAG, "onCreate: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));
//if (savedInstanceState == null) {
switch (position) {
case 1:
transaction.replace(R.id.fragment_container, Fragment1.newInstance(), FRAGMENT_TAG1).commit();
break;
case 2:
transaction.replace(R.id.fragment_container, Fragment2.newInstance(), FRAGMENT_TAG2).commit();
break;
case 3:
transaction.replace(R.id.fragment_container, Fragment3.newInstance("Terzo"), FRAGMENT_TAG3).commit();
break;
}
//}else{
//instantiateFragments(savedInstanceState);
//}
getSupportActionBar().setTitle(name);
}
return false;
}
});
}