Darko Petkovski
Darko Petkovski

Reputation: 3912

Pass values to tab fragment

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

Upvotes: 0

Views: 2422

Answers (3)

RoiBar
RoiBar

Reputation: 81

in the bundle (3ed parameter)

Bundle myBundle = new Bundle()
myBundle.putInt("paramKey", 1);
mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
                MyClipsFragment.class, myBundle);

Upvotes: 1

Mikelis Kaneps
Mikelis Kaneps

Reputation: 4584

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

Upvotes: 0

Panther
Panther

Reputation: 9408

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

Upvotes: 0

Related Questions