Android - Set fragment id

前端 未结 7 1576
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 15:22

How can I set a Fragment\'s Id so that I can use getSupportFragmentManager().findFragmentById(R.id.--)?

相关标签:
7条回答
  • 2020-11-27 15:55

    In most cases you can use the fragment tag as well as the ID.
    You can set the tag value in FragmentTransaction.add(Fragment fragment, String tag );. Then you can use the command FragmentManager.findFragmentByTag(String tab) to find the fragment in question.

    0 讨论(0)
  • 2020-11-27 15:55

    As Tom and others already mention, there are ways to put a tag on a fragment and use that tag for identification. A subsequent problem I've come across with those solutions is that the fragment doesn't get a tag until it's associated with the Activity (or, actually, the FragmentManager). What to do if one needs to identify a fragment before it has been tagged?

    My solutions so far all rely on the oldest (Java) trick in the world: create a minimalistic template fragment which takes an id in one of it's constructors and provides a getFragmentId() method which returns that id. I then let those fragments that need early identification extend that template and; voila! Problem solved.

    This solution might, unfortunately, require a set of template fragments, one for each fragment type, ListFragment, DialogFragment or plain old Fragment (POFO?!) that need early identification. But this is manageable in the case of fragments I think, considering the gains provided.

    Sorry for tearing up healing wounds :-)

    Cheers!

    0 讨论(0)
  • 2020-11-27 16:04

    You can't set a fragment's ID programmatically.

    There is however, a String tag that you can set inside the FragmentTransaction which can be used to uniquely identify a Fragment.

    As Aleksey pointed out, you can pass an ID to FragmentTransaction's add(int, Fragment) method. However, this does not specify the ID for a Fragment. It specifies the ID of a ViewGroup to insert the Fragment into. This is not that useful for the purpose I expect you have, because it does not uniquely identify Fragments, but ViewGroups. These IDs are of containers that one or more fragments can be added to dynamically. Using such a method to identify Fragments would require you to add ViewGroups dynamically to the Layout for every Fragment you insert. That would be pretty cumbersome.

    So if your question is how to create a unique identifier for a Fragment you're adding dynamically, the answer is to use FragmentTransaction's add(int containerViewId, Fragment fragment, String tag) method and FragmentManager's findFragmentByTag(String) method.

    In one of my apps, I was forced to generate strings dynamically. But it's not that expensive relative to the actual FragmentTransaction, anyway.

    Another advantage to the tag method is that it can identify a Fragment that isn't being added to the UI. See FragmentTransaction's add(Fragment, String) method. Fragments need not have Views! They can also be used to persist ephemeral state between config changes!

    0 讨论(0)
  • 2020-11-27 16:07

    Use the following:

    To add a fragment:

    getFragmentManager().beginTransaction().add(R.id.fragment_container, fragmentToBeAdded, tag).commit();
    

    To identify existing fragment:

    getFragmentManager().findFragmentByTag(fragmentName);
    
    0 讨论(0)
  • 2020-11-27 16:08

    When using a tag please do make sure to add the

    fragmentTransaction.addToBackStack(null); 
    

    method so that your Fragment is resumed instead of destroyed as mentioned in the developer guides.

    If you don't call addToBackStack() when you perform a transaction that removes a fragment, then that fragment is destroyed when the transaction is committed and the user cannot navigate back to it. Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and is later resumed if the user navigates back.

    You can find this at the end of this page.

    I lost about 30 minutes trying to figure out why my Fragment was not being found through a simple findFragmentByTag(); call

    0 讨论(0)
  • 2020-11-27 16:18

    In addition to Tom's answer, replace method also supports the fragment tag, in addition to add method.

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