getTag() always returns null when called in fragment
Asked Answered
J

1

8

I have a problem, because getTag() method returns me null when I call it in fragment which is a part of a ViewPager Tab Layout I created.

Code

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class HistoryFragment extends Fragment {
    ListView listView;
    HistoryAdapter adapter;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View history = inflater.inflate(R.layout.fragment_history, container, false);
        ArrayList<ToSave> arrayOfData = new ArrayList<ToSave>();
        adapter = new HistoryAdapter(getActivity().getBaseContext(), arrayOfData);
        listView = (ListView) history.findViewById (R.id.listView1);
        listView.setAdapter(adapter);
        String myTag = getTag();
        ((MainActivity)getActivity()).setHistoryFragment(myTag);
        Toast.makeText(getActivity(), "HistoryFragment.onCreateView(): " + myTag, Toast.LENGTH_LONG).show();
        return history;
    }
}

I want to use it for communication between Fragments (add ListView items by clicking a button in another fragment), but I can't make it work.

edit

TabPagerAdapter.java

      @Override
      public Fragment getItem(int i) {
        switch (i) {
            case 0:
                if (mFragmentAtPos0 ==null) {
                    mFragmentAtPos0 = new PartyFragment(listener);
                }
                return mFragmentAtPos0;
            case 1:
                return new SummaryFragment();
            case 2:
                return new HistoryFragment();
            }
        return null;
      }
Johnny answered 14/8, 2014 at 17:17 Comment(3)
Where do you set tag for your fragment?Raptorial
Which class exactly TabPagerAdapter extends? Are you also overridig getItemId()?Raptorial
I assumed you are using one of the Fragment Adapters and updated my answer accordingly.Raptorial
R
15

Fragment tags are not automatically specified. You have to assign them yourself. There are several places to do it depending on how you attach fragment: in the XML or dynamically.

If it's define in XML then you can set it like this:

<fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:tag="your_tag"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

If you add fragment dynamically then you can do it like this:

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment, "your_tag");
fragmentTransaction.commit();

In both examples look for "your_tag".

Then when you call getTag() on you Fragment you will get "your_tag" as result.


In case use serve your fragments to FragmentPagerAdapter, tags are automatically assigned based on getItemId(int) which by default returns pager position. So calling getTag() would return fragment position in ViewPager.


In case using FragmentStatePagerAdapter fragments tags are NOT specified. If that's the case you have to switch to first adapter type or use another way to refer your fragments.

From your adapter's implementation I know that you have only 3 pages, so FragmentPagerAdapter is more appropriate for you.

As a proof this is a part of description from FragmentStatePagerAdapter:

This version of the pager is more useful when there are a large number of pages, working more like a list view.

and next description from FragmentPagerAdapter:

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs.


If you insist using FragmentStatePagerAdapter keep reading:

I assume you need tags to find your fragments later on. Instead tags keep a reference to the fragment. Your Activity's method ((MainActivity)getActivity()).setHistoryFragment(myTag); already expect a fragment of specific kind. So you could call it like this ((MainActivity)getActivity()).setHistoryFragment(this); Later, instead of searching fragment, check if it's not null and use it.

Raptorial answered 14/8, 2014 at 17:29 Comment(5)
Please, look at my edited post. I don't know how to add the tag in the code I haveJohnny
I am using FragmentStatePagerAdapter, but even after Overriding onCreate() method like in your post, getTag() still returns null.Johnny
@DamianPetla How would you set a tag for a fragment in XML which isn't designed using the <fragment ... > syntax? E.g My fragment is inflated using a layout XML file programmattically. Is there any way to set the tag using this method in the onCreateView?Passel
@Chris-Jr Fragment's tag has package visibility and is used only by FragmentManager. I don't know why you need tag for anything else. Maybe explain your end goal and I will try to find solution, would be perfect if you post a new question.Raptorial
@DamianPetla I actually had figured out what the problem, and the tag began to work. Unfortunately I can't remember what it was now, but thanks for your help.Passel

© 2022 - 2024 — McMap. All rights reserved.