Handle Fragment On Screen Orientation Changes?

2019-06-10 06:35发布

I am useing This Sort of Code To Handle Three Fragment in Main Activity ...

FragmentA is Fixed it One Frame .. I change FragmentB and FragmentC on Button Click on FragmentA. his Code is Running well either in Portrait or Landscape view .Here is The Code bellow. enter image description here

public class MainActivity extends FragmentActivity implements
        OnSwitchClickListener {

    FragmentManager manager;
    FragmentA fragA;
    FragmentB fragB;
    FragmentC fragC;

    boolean fragBSet = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragA = new FragmentA();
        fragB = new FragmentB();
        fragC = new FragmentC();

        manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();

        ft.add(R.id.a_container, fragA, "frag A");

        if (fragBSet) {
            ft.add(R.id.bc_container, fragC, "frag C");
        } else {
            ft.add(R.id.bc_container, fragB, "frag B");
        }
        fragBSet = true;
        ft.commit();

    }

    @Override
    public void onSwitchClick(View v) {
        Toast.makeText(getApplicationContext(), "Switch clkick from Activity",
                Toast.LENGTH_LONG).show();
        FragmentTransaction ft = manager.beginTransaction();
        if (fragBSet) {
            ft.remove(fragB);
            ft.add(R.id.bc_container, fragC, "frag C");
            fragBSet = false;
        } else {
            ft.remove(fragC);
            ft.add(R.id.bc_container, fragB, "frag B");
            fragBSet = true;
        }

        ft.commit();

    }

This Code is Running well either in Portrait or Landscape view ... But When Ever I change the Orientation The Two Fragments Override One Another.

Need Solution.

1条回答
趁早两清
2楼-- · 2019-06-10 07:25

When your orientation change, your Activity is recreated.

"In the scenario where a user rotates their device, Android will destroy your 
application’s activity(s).  Before destroying them it calls, onSaveInstanceState,
allowing developers to persist data.  Once the activity is recreated post 
rotation, the OS will call onRestoreInstanceState giving developers a chance to 
restore the application state pre-rotation."  

You should try to save your Fragments State.

putFragment: Put a reference to a fragment in a Bundle. This Bundle can be persisted as saved state, and when later restoring getFragment(Bundle, String) will return the current instance of the same fragment.

@Override
protected void onSaveInstanceState(Bundle outState) {
   FragmentManager manager = getFragmentManager();
   manager.putFragment(outState, MyFragment.TAG, mMyFragment);
}  

getFragment: Retrieve the current Fragment instance for a reference previously placed with putFragment(Bundle, String, Fragment).

private void instantiateFragments(Bundle inState) {
   FragmentManager manager = getFragmentManager();
   FragmentTransaction transaction = manager.beginTransaction();

   if (inState != null) {
      mMyFragment = (MyFragment) manager.getFragment(inState, MyFragment.TAG);
   } else {
      mMyFragment = new MyFragment();
      transaction.add(R.id.fragment, mMyFragment, MyFragment.TAG);
      transaction.commit();
   }
}  

restoreFragment:

@Override
protected void onRestoreInstanceState(Bundle inState) {
   instantiateFragments(inState);
}  

Hope this helps.

查看更多
登录 后发表回答