Handle Fragment On Screen Orientation Changes?

女生的网名这么多〃 提交于 2019-12-08 13:35:08

问题


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.

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:


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.



来源:https://stackoverflow.com/questions/20189151/handle-fragment-on-screen-orientation-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!