Kapparino
Kapparino

Reputation: 988

Proper place to set fragment tag

When I want to open a new fragment I call this method (which normally works):

openFragment(getActivity(), R.id.fragment_holder_id, MyFragment.newInstance());

Problem I'm dealing with is fragment tag which is always returning null, before and after commmit.

public static void openFragment(Activity activity, int fragmentHolderId, Fragment fragment) {
    FragmentTransaction fragmentTransaction = activity.getFragmentManager().beginTransaction();
    System.out.println("Tag Before Commit: " + fragment.getTag()); // null
    fragmentTransaction.replace(fragmentHolderId, fragment, fragment.getTag());
    fragmentTransaction.commit();
    System.out.println("Tag After Commit: " + fragment.getTag()); // null
}

Where is proper place to set fragment tag ?

Upvotes: 0

Views: 301

Answers (1)

N J
N J

Reputation: 27535

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

Upvotes: 2

Related Questions