Fragments displayed over each other

前端 未结 5 1671
北荒
北荒 2020-12-31 01:09

I\'ve got an Activity with a DrawerLayout, using the guidelines from http://developer.android.com/training/implementing-navigation/nav-drawer.html.

When I click on a

相关标签:
5条回答
  • 2020-12-31 01:28

    let's try with

    fragmentManager.beginTransaction().executePendingTransactions();
    

    after you call commit() method

    0 讨论(0)
  • 2020-12-31 01:31

    We went live with this version and havent received any complaints about this, so I will assume this was the correct answer:

    in your onCreateView method add:

    if (container != null) {
        container.removeAllViews();
    }
    

    Be sure to check if container is not null!

    Thanks https://stackoverflow.com/users/2677588/lia-pronina!

    0 讨论(0)
  • 2020-12-31 01:33

    After about 1 week, I found the solution without adding background color or anything else. Just add this code and fix that bullshit. I hope it will help all of you.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        container.clearDisappearingChildren();
        return inflater.inflate(R.layout.fragment, container, false);
    }
    
    0 讨论(0)
  • 2020-12-31 01:34

    I run in this same problem and I see that there's already an accepted answer but these answer is not 100% right and didn't fix my problem. The proposed answer by @Niels removes the views but the fragment(s) is(are) still added. This is what I am using:

    /**
     * Call this to remove all the other added fragments and keep only the current one.
     *
     * @param activity the activity to which the fragment has been attached.
     * @param fragment the fragment we want to keep.
     */
    public static void removeOtherAddedFragments(@NonNull AppCompatActivity activity, @NonNull Fragment fragment) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        for (Fragment frag : fragmentManager.getFragments()) {
            if (frag != null && !frag.equals(fragment) && frag.isAdded()) {
                fragmentManager.beginTransaction().remove(frag).commit();
            }
        }
    }
    

    I am calling this in my onResume to be sure that it will be called also when I navigate back to the fragment.

    0 讨论(0)
  • 2020-12-31 01:39

    Add in the every layout

    android:background="#FFFFFF"
    

    The layouts background in default are transparen, so just put a background color and the new elements fragment, not display over old fragment

    0 讨论(0)
提交回复
热议问题