150

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?

Macarse
  • 91,829
  • 44
  • 175
  • 230
Axel M. Garcia
  • 5,138
  • 9
  • 27
  • 29

8 Answers8

124

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.

PJL
  • 18,735
  • 17
  • 71
  • 68
  • 2
    Your answer lies [here][1] in the post on stackoverflow [1]: http://stackoverflow.com/questions/9363072/android-set-fragment-id – Chaitanya K Jul 26 '12 at 06:04
  • 2
    Use FragmentTransaction's add(int containerViewId, Fragment fragment, String tag) as described here: http://stackoverflow.com/a/13244471/4002895 @PJL Please edit your answer.This answer misleading people – dasar Nov 26 '14 at 12:56
  • That's pretty inconvenient. – Ian Wambai May 30 '17 at 21:36
79

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(); 
David
  • 37,109
  • 32
  • 120
  • 141
38

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

auspicious99
  • 3,902
  • 1
  • 44
  • 58
Kuool
  • 621
  • 5
  • 2
  • 38
    That would work if one was using a layout file. But this question refers to setting the tag dynamically in Java. – IgorGanapolsky May 11 '13 at 15:17
  • 1
    this was the answer I needed because sometimes while using some libraries, you don't have control over fragment transactions so you can't programmatically set the tag. Thanks! – usernotnull Dec 16 '16 at 10:09
2

You can also get all fragments like this:

For v4 fragmets

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

For app.fragment

List<Fragment> allFragments = getFragmentManager().getFragments();
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Chris Fremgen
  • 4,649
  • 1
  • 26
  • 26
2

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");
        }
    }
}

Answer by : https://www.kproapps.com

Kingsley Mitchell
  • 2,412
  • 2
  • 18
  • 25
1

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 :)

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
0

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);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Th3Wolf
  • 149
  • 1
  • 10
  • The question wasn't about setting a property on a Fragment. [It was about being able to retrieve an existing Fragment instance without keeping a reference around](https://developer.android.com/reference/androidx/fragment/app/FragmentManager#findFragmentByTag(java.lang.String)). – mkuech Mar 25 '20 at 08:19
  • @Th3Wolf, Thanks. Your answer solved my problem.... – Smack Alpha Nov 14 '20 at 15:02
-24

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);
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
farid_z
  • 1,673
  • 21
  • 11