How to Set a Fragment Tag by Code

How to set a Fragment tag by code?

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.

How to set a Tag to a Fragment in Android

You can set a Tag during fragment transaction.

For example if it's a replace transaction you could do it like so:

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, mFragment, TAG)
.commit();

If the Fragment you're using is not from Support Library, use getFragmentManager() instead of getSupportFragmentManager().

Set and get using tag a fragment in android

You are not setting tag for fragment that is the reason you get null

Instead of using,

FavoritesFragment favoritesFragment = (FavoritesFragment) getSupportFragmentManager()
.findFragmentByTag(getString(R.string.fragment_favorite_tag));

use this,

FavoritesFragment favoritesFragment = (FavoritesFragment) getSupportFragmentManager()
.getFragments()
.get(0);

to get instance of FavoritesFragment.

I have put get(0) as your position of FavoritesFragment instance is at zero.
You can get AirTodayFragment instance at position 1 calling get(1)

Uses of fragment tags

Fragment tags can be used to avoid recreating a Fragment on Activity orientation change.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_image_pager);

MyFragment fragment;
if (savedInstanceState != null) {
fragment = (MyFragment) getFragmentManager()
.findFragmentByTag("my_fragment_tag");
} else {
fragment = new MyFragment();
fragment.setArguments(getIntent().getExtras());
getFragmentManager()
.beginTransaction()
.add(android.R.id.content, fragment, "my_fragment_tag")
.commit();
}
}

The Activity is recreated on orientation change, and its onCreate(...) method is called. If the Fragment was created before the Activity was destroyed and was added to the FragmentManager with a tag, it can now be retrieved from the FragmentManager by the same tag.

For a longer discussion on how it can be used, see:

  • ViewPager and fragments - what's the right way to store fragment's state?

Proper place to set fragment tag

Use of TAG is to identify fragment uniquely from pool of fragment in transaction .

So while replace() set TAG

From which you can access fragment later by following code

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

How to get fragment tag or ID?

For this type of condition i create a function inside fragment which will return me the instance of fragment and make the fragment constructor private something like:-

public class LeftFragmentClass extends Fragment{
private String fragmentTag = null;

public LeftFragmentClass(){}

public static LeftFragmentClass newInstance(String tag){
LeftFragmentClass mLeftFragmentClass = new LeftFragmentClass();
Bundle bundle = new Bundle();
bundle.putString ("tag",tag);
mLeftFragmentClass.setArgument(bundle);

return mLeftFragmentClass;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
tag = getArguments().getString("tag")
}

}

So i used newInstance function to create instance of LeftFragmentClass and pass the tag to it which i m setting to Fragment argument using bundle and inside onCreate get bundle using getArguments and from it the tag value. Pass this tag value as one of the parameter to your callback method to identify which button was clicked.

So from activity for getting instance of LeftFragmentClass you can write as

LeftFragmentClass mLeftFragmentClassLeft = LeftFragmentClass.newInstance("left")

LeftFragmentClass mLeftFragmentClassRight = LeftFragmentClass.newInstance("Right")

==== Edit ====

keep the fragment class constructors always public don't make it private as i suggested above in my sample code. Making it private will cause application to crash with exception

java.lang.RuntimeException: Unable to start activity
ComponentInfo{MainActivity}:
android.support.v4.app.Fragment$InstantiationException: Unable to
instantiate fragment com.thatswhy.AppAlertDialog: make sure class name
exists, is public, and has an empty constructor that is public

Set and call Fragment Tag correct

Call executePendingTransactions() on fragment manager after committing transaction

...
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold();

EDIT 1

You can try another option:

  1. Do not add the executePendingTransactions()

  2. In fragment's onViewCreated, if getTag() mathes "fragment01" call unfold method



Related Topics



Leave a reply



Submit