Find Fragment by tag name in Container

前端 未结 6 584
耶瑟儿~
耶瑟儿~ 2020-12-03 14:13

I have a relative layout and i am adding fragment in that relative layout. Like this

     HomeFragment mHomeFragment = new HomeFragment();
    FragmentMana         


        
相关标签:
6条回答
  • 2020-12-03 15:00

    If you want to get the tag of a fragment you can use the getTag() method. This probably isn't going to be the nicest way to achieve what you are looking to do.

    http://developer.android.com/reference/android/app/Fragment.html#getTag()

    If you have several fragments and you will change them around, consider using an adapter.

    0 讨论(0)
  • 2020-12-03 15:01
    Fragment fragment = getFragmentManager().findFragmentById(R.id.order_container);
    String tag = (String) fragment.getTag();
    
    0 讨论(0)
  • 2020-12-03 15:01

    You can use findFragmentById in FragmentManager

    getFragmentManager().findFragmentById(R.id.order_container)
    
    0 讨论(0)
  • 2020-12-03 15:03
    getSupportFragmentManager().findFragmentById(R.id.container).getClass().getName();
    
    0 讨论(0)
  • 2020-12-03 15:07

    You want the name?? You can get your fragment using the ID and doing:

    fragment.getClass().getName();
    

    is it what you want? Or you can use:

    fragment.getTag();
    

    it's returns the tag name of the fragment, if specified.

    0 讨论(0)
  • 2020-12-03 15:13

    you can retrieve a Fragment by tag through

    Fragment fragment = getFragmentManager().findFragmentByTag("yourstringtag");
    

    then you can check if your fragment is instace of HomeFragment

    if (fragment instanceof HomeFragment) {
    }
    
    0 讨论(0)
提交回复
热议问题