0

I have two tabs in a FragmentTabHost - and my question is how can I pass data to the selected fragment?

Here is my code:

mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
                MyClipsFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("clipboards").setIndicator("Clipboards"),
                FragmentSearchMyClipboards.class, null);
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • Duplicate of http://stackoverflow.com/questions/23467612/communication-between-fragments-of-fragment-tab-host – Lukas Nov 05 '14 at 17:22

3 Answers3

1

in the bundle (3ed parameter)

Bundle myBundle = new Bundle()
myBundle.putInt("paramKey", 1);
mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
                MyClipsFragment.class, myBundle);
Mohsin
  • 1,586
  • 1
  • 22
  • 44
RoiBar
  • 81
  • 7
0

Actually the problem comes down to communicate between two fragments. The only way is through the activity which holds both the fragment. This can be done via declaring one interface in both the fragments and implementing them in the activity. Try to read here http://developer.android.com/training/basics/fragments/communicating.html

Panther
  • 8,938
  • 3
  • 23
  • 34
0

The easiest way to communicate through fragments is using EventBus - https://github.com/greenrobot/EventBus

HowTo: 1. Add these line to fragment that needs to get the information:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return inflater.inflate(R.layout.event_list, container, false);

    }

@Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
public void onEventMainThread(EventBusMsg bus) { //Name your method like this   
        //here you get the info...
    }

2. Post the information from anywhere, like this:

EventBus.getDefault().post(new EventBusMsg(true));
  1. Make class of the object you are going to post:

    public class EventBusMsg {
    

    //some info... }

Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48