我正在使用

导航抽屉 Activity
创建新项目时在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);
}

}

如代码片段所示,当我使用导航项目中的一项时,我使用的是碎片数,它将替换当前的碎片

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

我怎样才能做到这一点 ?

最佳答案

您保存实例状态的逻辑几乎是正确的。您只需要在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
  }
}

让我知道这个是否奏效。

07-24 09:46