0

I am using fragment in my application. When I change the orientation the fragment removes and my main activity gets visible from which I have added this fragment. below is the code -

Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new ManageDataFragment();
        break;
    case 1:
        fragment = new DownloadFragment();
        break;
    case 2:
        fragment = new UserInfoFragment();
        break;
    case 3:
        fragment = new AboutAppFragment();
        break;
    case 4:
        fragment = new ShareAppFragment();
        break;
    case 5:
        fragment = new SettingsFragment();
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
        // update selected item and title, then close the drawer
        setTitle(menutitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }

I am using this fragment from navigationDrawer like in Gmail application. What should I do that my fragment remains even I change the orientation of device?

Thanks in Advance

unflagged.destination
  • 1,576
  • 3
  • 19
  • 38

3 Answers3

1

I had exactly the same problem as mentioned above and I fixed it in the AndroidManifest by adding this:

 <activity
        ...
        android:configChanges="orientation|screenLayout|screenSize|layoutDirection">

Hope this helps!

Robin
  • 709
  • 2
  • 8
  • 20
  • @unflagged.destination This is not a solution, it's more like a hack.Check out this answer http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation – LordRaydenMK Jun 24 '14 at 07:37
1

I don't think the accepted answer is the correct so I'm writing this one:

When replacing the fragment you should use the method replace with three arguments so you can find your fragment later:

fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment, TAG_FRAGMENT).commit();

Where TAG_FRAGMENT is some string tag.

Then in onCreate if the activity was restarted you can find your fragment and add it to the container:

if(savedInstanceState != null) {
    fragment = getFragmentManager().findFragmentByTag(TAG_FRAGMENT);
    fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment, TAG_FRAGMENT).commit();
}
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • Yes, Its working. But I am facing another problem now that when I click on back button on fragment the activity through which I have added this fragment gets closed. I want to go to the activity when user clicks on back button on fragment. Is there any property for that? – unflagged.destination Jun 24 '14 at 08:12
  • You can use the method `addToBackStack` form the `FragmentTransaction`. More about navigation here: http://developer.android.com/training/implementing-navigation/temporal.html – LordRaydenMK Jun 24 '14 at 08:14
0

Probably should save position using onSaveInstanceState() and test the bundle argument to onCreate().

guycole
  • 788
  • 5
  • 10