How to set a Fragment tag by code?

AndroidAndroid 3.0-HoneycombAndroid Fragments

Android Problem Overview


I haven't found something like setTag(String tagName) method in the Fragment class. The only way to set a Fragment tag that I have found is by doing a FragmentTransaction and passing a tag name as parameter.

Is this the only way to explicitly set a Fragment tag by code?

Android Solutions


Solution 1 - Android

Yes. So the only way is at transaction time, e.g. using add, replace, or as part of the layout.

I determined this through an examination of the compatibility sources as I briefly looked for similar at some point in the past.

Solution 2 - Android

You can set tag to fragment in this way:

Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
    .replace(R.id.MainFrameLayout,fragmentA,"YOUR_TARGET_FRAGMENT_TAG")
    .addToBackStack("YOUR_SOURCE_FRAGMENT_TAG").commit(); 

Solution 3 - Android

You can provide a tag inside your activity layout xml file.

Supply the android:tag attribute with a unique string.

Just as you would assign an id in a layout xml.

    android:tag="unique_tag"

link to developer guide

Solution 4 - Android

You can also get all fragments like this:

For v4 fragmets

List<Fragment> allFragments = getSupportFragmentManager().getFragments();

For app.fragment

List<Fragment> allFragments = getFragmentManager().getFragments();

Solution 5 - Android

This is the best way I have found :

   public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
          // Let's first dynamically add a fragment into a frame container
          getSupportFragmentManager().beginTransaction(). 
              replace(R.id.flContainer, new DemoFragment(), "SOMETAG").
              commit();
          // Now later we can lookup the fragment by tag
          DemoFragment fragmentDemo = (DemoFragment) 
              getSupportFragmentManager().findFragmentByTag("SOMETAG");
        }
    }
}

Solution 6 - Android

Nowadays there's a simpler way to achieve this if you are using a DialogFragment (not a Fragment):

val yourDialogFragment = YourDialogFragment()
yourDialogFragment.show(
    activity.supportFragmentManager,
    "YOUR_TAG_FRAGMENT"
)

Under the hood, the show() method does create a FragmentTransaction and adds the tag by using the add() method. But it's much more convenient to use the show() method in my opinion.

You could shorten it for Fragment too, by using a Kotlin Extension :)

Solution 7 - Android

I know it's been 6 years ago but if anyone is facing the same problem do like I've done:

Create a custom Fragment Class with a tag field:

public class MyFragment extends Fragment {
 private String _myTag;
 public void setMyTag(String value)
 {
   if("".equals(value))
     return;
   _myTag = value;
 }
 //other code goes here
}

Before adding the fragment to the sectionPagerAdapter set the tag just like that:

 MyFragment mfrag= new MyFragment();
 mfrag.setMyTag("TAG_GOES_HERE");
 sectionPagerAdapter.AddFragment(mfrag);

Solution 8 - Android

You can add the tag as a property for the Fragment arguments. It will be automatically restored if the fragment is destroyed and then recreated by the OS.

Example:-

	final Bundle args = new Bundle();
	args.putString("TAG", "my tag");
	fragment.setArguments(args);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAxel M. GarciaView Question on Stackoverflow
Solution 1 - AndroidPJLView Answer on Stackoverflow
Solution 2 - AndroidDavidView Answer on Stackoverflow
Solution 3 - AndroidKuoolView Answer on Stackoverflow
Solution 4 - AndroidChris FremgenView Answer on Stackoverflow
Solution 5 - AndroidKingsley MitchellView Answer on Stackoverflow
Solution 6 - Androidxarlymg89View Answer on Stackoverflow
Solution 7 - AndroidTh3WolfView Answer on Stackoverflow
Solution 8 - Androidfarid_zView Answer on Stackoverflow