1

I have two fragments MainFragment and First_Fragment. First fragment contains a video view but whenever I try to rotate, nav drawer always goes back to its main fragment. How could I save or restore my fragment state ? so it would stop going back to its main fragment.

Here's my code for the MainActivity

    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

VideoView videoView;
NavigationView navigationView = null;
Toolbar toolbar = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    MainFragment first = new MainFragment();
    FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, first);
    fragmentTransaction.commit();
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);



}
    public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
        MainFragment first = new MainFragment();
        FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, first);

        fragmentTransaction.commit();

    } else if (id == R.id.nav_gallery) {
        First_Fragment first = new First_Fragment();
        FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, first);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_slideshow) {

    }

4 Answers4

3

When you're not handling the activity change like orientation, keyboard, etc. it will automatically recreates the activity. Therefore you're fragment does not save your previous instance. To solve that, I only encounter two solution.

The first one is already mentioned by Zeeshan that handle your orientation change by following this step:

  1. Handle the orientation change in your AndroidManifest

    <activity
        android:name="com.example.Activity"
        android:label="Activity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="stateHidden|adjustResize"
        android:theme="@style/Theme.AppCompat.Light" />
    
  2. Handle the event in the onConfiguration Change method

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
    }
    

The second one is to save the fragment state. Refer here https://stackoverflow.com/a/17135346/5870896

Community
  • 1
  • 1
Rashid
  • 1,700
  • 1
  • 23
  • 56
1

You can do setRetainInstance(true) in your fragments, but a BottomNavigationBar will work incorrectly. In this case you can use some boolean flag in activity and save/restore it with Bundle like this:

In onCreate:

if (savedInstanceState != null) {
        isMapFragmentVisible = savedInstanceState.getBoolean("isMainVisible");
    } else {
        navigator.navigateToFragment(fragmentLocation);
    }

In onSavedInstanceState:

outState.putBoolean("isMainVisible", isMapFragmentVisible);

In onNavigationSelectedListener:

case R.id.bottom_navigation_map:
    if (isMapFragmentVisible) {
        break;
    } else {
        navigator.navigateToFragment(fragmentMap);
        isMapFragmentVisible = true;
    }
    return true;

case R.id.bottom_navigation_location:
    if (!isMapFragmentVisible) {
        break;
    } else {
        navigator.navigateToFragment(fragmentLocation);
        isMapFragmentVisible = false;
    }
    return true;

For me it works perfect. Are there any more elegant ways?

Neuron
  • 1,020
  • 2
  • 13
  • 28
0

Activity recreates when you rotate the device. you need to mention in your manifest that you will handle the orientation change in your code so that system don't do it like this:

<activity name="MainActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>

and then handle the event

@Override
public void onConfigurationChanged(Configuration newConfig) {

}

in your activity

zeshanbaig786
  • 157
  • 11
0

Write this in manifests.xml

 android:configChanges="orientation"

like this:-

<activity
        android:name=".fragmentLayouts.MainActivity"
        android:configChanges="orientation" />

Add this to your java code:-

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    drawerListener.onConfigurationChanged(newConfig);
}
Manmohan Kumar
  • 334
  • 2
  • 8