fragon
fragon

Reputation: 3471

getTag() always returns null when called in fragment

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

Upvotes: 8

Views: 16753

Answers (1)

Damian Petla
Damian Petla

Reputation: 9103

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.

Upvotes: 15

Related Questions