我正在创建一个导航抽屉,每个东西都在顺利运行:我成功创建它但是当我旋转方向时,我当前选择的片段会在旋转后自动附加到活动。
我没有维护onSaveInstanceState
。
例如:如果我来自垂直方向
fragmentA
- > fragmentB
- > fragmentC
并旋转方向
我得到了fragmentC
。
我没有保存活动状态。结果应为fragmentA
。
为什么会这样?
BaseFragment.java
public abstract class BaseFragment extends Fragment {
protected static final String ARG_SECTION_NUMBER = "section_number";
abstract public int getFragmentSectionNumber();
}
FragmentA.java
public class FragmentA extends BaseFragment {
public final static String TAG = FragmentA.class.getSimpleName();
public static FragmentA newInstance(int sectionNumber){
FragmentA fragmentA = new FragmentA();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER,sectionNumber);
fragmentA.setArguments(bundle);
return fragmentA;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
int sectionNumber = getFragmentSectionNumber();
((MainActivity)getActivity()).onSectionAttached(sectionNumber);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.layout_a, null, false);
return root;
}
@Override
public int getFragmentSectionNumber() {
return getArguments().getInt(ARG_SECTION_NUMBER);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private FragmentManager fragmentManager;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;
private Toolbar toolbar;
private NavigationView navigationView;
private String mTitle;
private int mCurrentPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
if (savedInstanceState == null) {
setFragment(FragmentA.newInstance(R.id.a));
} else {
// int id = savedInstanceState.getInt(Constants.SELECTED_NAVIGATION_ID);
// onSectionAttached(id);
}
navigationView.setNavigationItemSelectedListener(this);
}
private void init() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fragmentManager = getSupportFragmentManager();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
}
private void setFragment(BaseFragment baseFragment) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, baseFragment);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
public void onBackPressed() {
/*DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
return;
} else {
super.onBackPressed();
}
if (fragmentManager.getBackStackEntryCount() <= 0) {
super.onBackPressed();
return;
}
BaseFragment baseFragment = (BaseFragment) fragmentManager.getFragments().get(fragmentManager.getBackStackEntryCount() - 1);
if (baseFragment != null) {
mCurrentPosition = baseFragment.getFragmentSectionNumber();
onSectionAttached(mCurrentPosition);
}*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.a:
if (mCurrentPosition != R.id.a) {
setFragment(FragmentA.newInstance(R.id.a));
item.setChecked(true);
}
break;
case R.id.b:
if (mCurrentPosition != R.id.b) {
setFragment(FragmentB.newInstance(R.id.b));
item.setChecked(true);
}
break;
case R.id.c:
if (mCurrentPosition != R.id.c) {
setFragment(FragmentC.newInstance(R.id.c));
item.setChecked(true);
}
break;
case R.id.d:
if (mCurrentPosition != R.id.d) {
setFragment(FragmentD.newInstance(R.id.d));
item.setChecked(true);
}
break;
case R.id.e:
if (mCurrentPosition != R.id.e) {
setFragment(FragmentE.newInstance(R.id.e));
item.setChecked(true);
}
break;
default:
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void onSectionAttached(int number) {
switch (number) {
case R.id.a:
mTitle = "Fragment A";
mCurrentPosition = R.id.a;
break;
case R.id.b:
mTitle = "Fragment B";
mCurrentPosition = R.id.b;
break;
case R.id.c:
mTitle = "Fragment C";
mCurrentPosition = R.id.c;
break;
case R.id.d:
mTitle = "Fragment D";
mCurrentPosition = R.id.d;
break;
case R.id.e:
mTitle = "Fragment E";
mCurrentPosition = R.id.e;
break;
default:
return;
}
if (mTitle != null && getSupportActionBar() != null) {
getSupportActionBar().setTitle(mTitle);
navigationView.getMenu().findItem(mCurrentPosition).setChecked(true);
}
}
private static class Constants {
final static String SELECTED_NAVIGATION_ID = "selected_navigation_id";
}
}