hey guys Im trying to make an activity that has a one framelayout that changed when an Item in the drawer is selected, but my problem is when I rotate the screen the fragment that was replaced goes back to the previous fragment. ex. I opened the app and it shows me the fragment A then I try to select another fragment from the drawer which is fragment B but then when I try to rotate while Im on fragment B it goes back to fragment A. here is my code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
if(savedInstanceState != null){
getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}
switch (passedFragment){
case "com.serverus.oom.fragments.FragmentAgency":
fragmentClass = FragmentAgency.class;
menuItemReserve = mMenu.findItem(R.id.agency_menu_item);
break;
case "com.serverus.oom.fragments.FragmentServices2":
fragmentClass = FragmentServices2.class;
menuItemReserve = mMenu.findItem(R.id.services_menu_item);
break;
case "com.serverus.oom.fragments.FragmentContactUs":
fragmentClass = FragmentContactUs.class;
menuItemReserve = mMenu.findItem(R.id.contact_menu_item);
break;
default:
fragmentClass = FragmentAgency.class;
break;
}
fragmentReplace(fragmentClass);
}
public void fragmentReplace(Class fragmentClass) {
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment, FRAGMENT_TAG).addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
}
thanks in advanced