Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
      }
like image 513
fragon Avatar asked Aug 14 '14 17:08

fragon


People also ask

What is the difference between fragment and FrameLayout?

Show activity on this post. A framelayout, Relative View and a few others represents a view in android and is extended from viewgroup. A Fragment is a an an Object that is used to represent a portion of a user interface and is usually hosted in an activity. A fragment has a viewgroup which you can assign an XML layout.

What is FrameLayout in fragment?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

What is a fragment in Android?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


1 Answers

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.

like image 144
Damian Petla Avatar answered Oct 13 '22 08:10

Damian Petla