问题描述:

I am using the

Navigation Drawer Activity

that build in android studio when creating new project

public class MainActivity extends AppCompatActivity

implements NavigationView.OnNavigationItemSelectedListener {

Toolbar toolbar;

NavigationView navigationView;

DrawerLayout drawerLayout;

private static final String PREFERENCES_FILE = "mymaterialapp_settings";

private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";

private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

private boolean mUserLearnedDrawer;

private boolean mFromSavedInstanceState;

private int mCurrentSelectedPosition;

private MenuBuilder mMenu;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitle("main");

setSupportActionBar(toolbar);

if (savedInstanceState != null) {

mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);

mFromSavedInstanceState = true;

}

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);

Fragment squadFragment = new MainItems();

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

fragmentTransaction.replace(R.id.contaner,squadFragment,null);

fragmentTransaction.commit();

mCurrentSelectedPosition=0;

}

@Override

public void onBackPressed() {

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

if (drawer.isDrawerOpen(GravityCompat.START)) {

drawer.closeDrawer(GravityCompat.START);

} else {

super.onBackPressed();

}

}

@SuppressWarnings("StatementWithEmptyBody")

@Override

public boolean onNavigationItemSelected(MenuItem item) {

int id = item.getItemId();

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

Fragment fragment = null;

String name;

if (id == R.id.nav_camera) {

Fragment f=new MainItems();

fragmentTransaction.replace(R.id.contaner,f);

fragmentTransaction.commit();

name="Main";

mCurrentSelectedPosition=1;

toolbar.setTitle(name);

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

Fragment f =Docaans.newInstance("key1");

fragmentTransaction.replace(R.id.contaner,f);

fragmentTransaction.commit();

name="page 2";

mCurrentSelectedPosition=2;

toolbar.setTitle(name);

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

Fragment f=new MainFragment();

fragmentTransaction.replace(R.id.contaner,f);

fragmentTransaction.commit();

mCurrentSelectedPosition=3;

name="page 4";

toolbar.setTitle(name);

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

Fragment f=new Catalogat();

fragmentTransaction.replace(R.id.contaner,f);

fragmentTransaction.commit();

mCurrentSelectedPosition=4;

name="page n";

toolbar.setTitle(name);

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

drawer.closeDrawer(GravityCompat.START);

return true;

}

@Override

public void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);

}

@Override

protected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);

mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION, 0);

Menu menu = navigationView.getMenu();

menu.getItem(mCurrentSelectedPosition).setChecked(true);

}

}

as shown in code snip i am use number of fragments when i use one item from the navigation item it replace the current fragment

but when i am rotate the screen it start the main screen and i need to save instance for my fragment

How Can I do this ?

相关阅读:
Top