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