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.
Answer based on the version used in the question (MaterialDrawer v3.x.y). The answer will require some changes to work with the latest MaterialDrawer v4.x.y
If the application is created and you do not activate it the drawer will not fire the OnDrawerItemClickListener
. You can enable this via the withFireOnInitialOnClick(true)
flag. This will allow you to have the whole fragment switch logic within the OnDrawerItemClickListener
If not set you have to set the correct fragment on your own after orientation change.
You use this code for this:
// Set the current drawer item
result.setSelection(mCurrentDrawerItem);
Back to your OnDrawerItemClickListener
as far as your code you posted above shows you use the switch based on the R.string. via the getNameRes()
method. This will not really work as you do not set the name of the items via the resource but directly as string. .withName("FragmentA")
. you should use the same in both cases.
A better solution (and i highly suggest this) is that you switch your code and OnDrawerItemClickListener
to switch based on an identifier you set on the DrawerItems
result = new Drawer()
.withActivity(this)
.withToolbar(mToolbar)
.withActionBarDrawerToggle(true)
.addDrawerItems(
new PrimaryDrawerItem().withName("FragmentA").withIdentifier(1),
new PrimaryDrawerItem().withName("FragmentB").withIdentifier(2)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
if(drawerItem != null) {
Fragment f = null;
switch(drawerItem.getIdentifier) {
case 1:
f = FragmentA.newInstance();
break;
case 2:
f = FragmentA.newInstance();
break;
}
if (drawerItem instanceof Nameable) {
mToolbar.setTitle(MainActivity.this.getString(((Nameable) drawerItem).getNameRes()));
}
}
}
}
}).build();