I know that there are couple of answers for this particular issue, however none of them helped me solve it.
I am working on an application which has a Drawer Navigation (Mike Penz drawer navigation). By selecting any of the items, the user is navigated to the corresponding fragment. The fragments work perfectly, and keep their state on orientation change. However, one of the fragments has a button which replaces the current fragment with a new one. When the new fragment is on screen, and if the user changes the orientation of the device, the previous fragment is displayed.
Process: Drawer Item Select > Fragment A > Button > Fragment B > Change Orientation > Fragment A.
Edit: This is how the First Fragment is called: (switch)
case R.string.drawer_item_a:
FragmentA A = FragmentA.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, A)
.commit();
break;
This is how the Second Fragment is called:
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentB B = FragmentB.newInstance();
fm.beginTransaction()
.replace(R.id.container, B, String.valueOf(R.string.settings_pin_request))
.commit();
}
Drawer
result = new Drawer()
.withActivity(this)
.withToolbar(mToolbar)
.withActionBarDrawerToggle(true)
.addDrawerItems(
new PrimaryDrawerItem().withName("FragmentA"),
new PrimaryDrawerItem().withName("FragmentB")
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
if (drawerItem instanceof Nameable) {
mCurrentDrawerItem = position;
// Display appropriate fragment
switch (((Nameable) drawerItem).getNameRes()) {
case R.string.drawer_item_a:
FragmentA A = FragmentA.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, A)
.commit();
break;
case R.string.drawer_item_b:
FragmentA A = FragmentA.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, A)
.commit();
break;
}
mToolbar.setTitle(MainActivity.this.getString(((Nameable) drawerItem).getNameRes()));
}
}
}).build();
// Set the current drawer item
result.setSelection(mCurrentDrawerItem);
The onResume() method implements logic for user synchronization. Nothing is written for fragment states.