How to destroy Fragment?

前端 未结 5 1454
北恋
北恋 2020-12-13 17:21

I have one Activity. The Activity has two Fragments. Fragment A is Menu. Fragment B is Detail.

I try to Make other Fragment C

相关标签:
5条回答
  • 2020-12-13 17:38

    Use this if you're in the fragment.

    @Override
            public void onDestroy() {
                super.onDestroy();
    
                getFragmentManager().beginTransaction().remove((Fragment) youfragmentname).commitAllowingStateLoss();
    
            }
    
    0 讨论(0)
  • 2020-12-13 17:43

    If you are in the fragment itself, you need to call this. Your fragment needs to be the fragment that is being called. Enter code:

    getFragmentManager().beginTransaction().remove(yourFragment).commitAllowingStateLoss();
    

    or if you are using supportLib, then you need to call:

    getSupportFragmentManager().beginTransaction().remove(yourFragment).commitAllowingStateLoss();
    
    0 讨论(0)
  • 2020-12-13 17:44

    Give a try to this

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
    
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.remove((Fragment) object);
        trans.commit();
    
        super.destroyItem(container, position, object);
    }
    
    0 讨论(0)
  • 2020-12-13 17:58

    It's used in Kotlin

    appCompatActivity?.getSupportFragmentManager()?.popBackStack()
    
    0 讨论(0)
  • 2020-12-13 18:01

    If you don't remove manually these fragments, they are still attached to the activity. Your activity is not destroyed so these fragments are too. To remove (so destroy) these fragments, you can call:

    fragmentTransaction.remove(yourfragment).commit()
    

    Hope it helps to you

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