Fragments within Fragments

前端 未结 6 713
名媛妹妹
名媛妹妹 2020-11-22 09:47

I\'m wondering if this is actually a bug in the Android API:

I have a setup like so:

┌----┬---------┐
|    |         |
|  1 |    2    |
|    |┌------         


        
6条回答
  •  悲&欢浪女
    2020-11-22 10:13

    If you find your nested fragment not being removed or being duplicated (eg. on Activity restart, on screen rotate) try changing:

    transaction.add(R.id.placeholder, newFragment);
    

    to

    transaction.replace(R.id.placeholder, newFragment);
    

    If above doesn't help, try:

    Fragment f = getChildFragmentManager().findFragmentById(R.id.placeholder);
    
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    
    if (f == null) {
        Log.d(TAG, "onCreateView: fragment doesn't exist");
        newFragment= new MyFragmentType();
        transaction.add(R.id.placeholder, newFragment);
    } else {
        Log.d(TAG, "onCreateView: fragment already exists");
        transaction.replace(R.id.placeholder, f);
    }
    transaction.commit();
    

    Learnt here

提交回复
热议问题