简体   繁体   中英

Android Fragments Displaying On Top Of Each Other When Screen Orientation Changes

I had a navigation menu, which displays various modes of operation to the user. When the user selects a specific option, the activity is to load the corresponding fragment (Eg: Add Product should load the AddProduct fragment).

The loading works fine - I used FragmentManager together with FragmentTransaction to replace the current fragment with the new fragment. The problem comes in when the screen gets rotated - the current two fragments I have used, display on top of each other.

Here is the code I have set up for the NavigationDrawerListener (the part that does the loading):

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    DrawerLayout drawerLayout = (DrawerLayout) currentActivty.findViewById(R.id
            .homeDrawer);
    TextView heading = (TextView) currentActivty.findViewById(R.id.AppHeadingTextView);
    switch (position)
    {
        case 0:
            heading.setText("Home");
            drawerLayout.closeDrawers();
            UserMainScreenFragment homeScreen = new UserMainScreenFragment();
            FragmentManager homeManager = currentActivty.getFragmentManager();
            FragmentTransaction homeTransaction = homeManager.beginTransaction();
            homeTransaction.replace(R.id.contentArea, homeScreen);
            homeTransaction.commit();
            break;
        case 2:
            heading.setText("Add a Product");
            drawerLayout.closeDrawers();
            AddProductFragment newProductFragment = new AddProductFragment();
            FragmentManager manager = currentActivty.getFragmentManager();
            FragmentTransaction replace = manager.beginTransaction();
            replace.replace(R.id.contentArea, newProductFragment);
            replace.commit();
           break;
    }
}

您需要根据方向为所需的活动创建单独的布局。

You have to add different layout's (with same name) for both orientations, then Android will automatically inflate the suitable xml according to the orientation.

File structure will look like this,

res
 - drawable
 - layout                        // for portrait
    - layout_name.xml            
 - layout-land                   // for landscape
    - layout_name.xml            // same name should be used
 - .....

Find a sample solution here

In your activity you need to check for savedInstanceState being null to avoid adding the same fragments again:

//when the orientation changes, the savedInstanceState bundle is not null
if(savedInstanceState!=null){
//add your fragment to the layout
}

another thing, I noticed you reference the fragment manager through currentActivty.getFragmentManager();

maybe the currentActivty.getFragmentManager(); has an old reference to the activity that gets destroyed, you should also use getSupportFragmentManager() if you're using the support library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM