I am implementing tabs using FragmentTabHost of v4 support library.
My first tab contains the list of items.When I click on list item it should move to tab 2. My second tab shows the description of the list item.So I need to pass the list of the first tab and the index of list item clicked to the second tab.
I am currently using getters and setters at application level. But is there any other way to do so? What is the best way to do this?
First fragment-Tab-0
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try {
mCallback = (OnRadioSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
mCallback.onRadioSelected(dataList, position);
}
public interface OnRadioSelectedListener {
public void onRadioSelected(ArrayList<DataModel> playingList, int playingIndex);
}
Tabhost activity code:
@Override
public void onRadioSelected(ArrayList<DataModel> playingList, int playingIndex) {
mPlayingIndex = playingIndex;
mPlayingList = playingList;
fragmentTabHost.setCurrentTab(1);
Fragment2 frag = (Fragment2) getSupportFragmentManager().findFragmentByTag(getString(R.string.str));
if (frag != null) {
Bundle args = new Bundle();
args.putSerializable("Key_list", mPlayingList);
args.putInt("Key_current_index", mPlayingIndex);
frag.setArguments(args);
}
}
first time frag is null and second time it gives the exception
I know this question is a little old but if anyone is still interested in knowing an easy way to do this ,here i go. So the problem is sending data between two fragments right , so basically what you have to start with is create a function which returns a string in your Activity. Something like
This is just getter and setter for a String TabFragmantB. This will return you the tag of the Fragment to which you are sending data(Lets call it Fragment B).So the code in Fragment B will look something like this ,write it in the onCreateView section itself.
This code will return you the tag of Fragmant B to our activity.
Also in Fragmant B create a function which takes in the type of data you want to take in FragmantB. In this case
Now Half the work is done ,lets move to Fragmant A, Here it goes something like this.
Note that by changing the functions in Fragment A and B ,you can pass any type of data.
Hope this works for everyone ,because it did for me.
Feel free to make changes.
I recommend checking out this developer article on Communicating with other fragments: https://developer.android.com/training/basics/fragments/communicating.html
In short, you have your
Activity
implement an interface which all fragments use to communicate with each other. When a fragment needs to do something, it asks theActivity
to handle the behavior.So if
fragment1
wants to go tofragment2
,fragment1
callsYourActivity.goToFragment2()
(or whatever), andYourActivity
will handle it. eg:You need to ensure your
Activity
implements the interface, so check out the implementation ofFragment.onAttach(Activity activity)
in the "Define a Interface" section: https://developer.android.com/training/basics/fragments/communicating.html#DefineInterface