为NavigationView保存实例?

时间:2016-04-05 11:57:52

标签: android android-fragments navigation-drawer

我正在使用

  

导航抽屉活动   在创建新项目时在android studio中构建

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

}

如代码片段所示,当我使用导航项中的一个项目替换当前片段

时,我会使用片段数量

但是当我旋转屏幕时,它会启动主屏幕,我需要为我的片段保存实例

  

我该怎么做?

1 个答案:

答案 0 :(得分:0)

保存实例状态的逻辑非常正确。您只需要在Activtity的OnCreate方法中恢复它。

你可以这样做 -

if (savedInstanceState != null) {
    mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
   //Here you can use an if else block to inflate the desired fragment
  if (mCurrentSelectedPosition == R.id.nav_camera) {
     //ADD THE FRAGMENT YOU WANT HERE...
  } else if (mCurrentSelectedPosition == R.id.nav_docaans) {
     //REPLACE THE FRAGMENT HERE ...
  } else if (mCurrentSelectedPosition == R.id.nav_friday) {
    //DO WHAT YOU WANT HERE
  } else if (mCurrentSelectedPositon == R.id.nav_cat) {
    //YOUR LOGIC HERE AGAIN
  }
}

让我知道这是否有效。