FragmentTabHost & Fragments - How do I pass data between tabs?
Asked Answered
P

3

5

I have a MainActivity (FragmentActivity) that has a FragmentTabHost.

public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tabs);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("classA").setIndicator("Class A"),
            ClassA.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("classB").setIndicator("Class B"),
            ClassB.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("classC").setIndicator("Class C"),
            ClassC.class, null);
    }
}

ClassA, ClassB and ClassC are all Fragments (android.support.v4.app.Fragment).

I need to pass data (and call methods) on the Fragments. How can I get a reference of each of the Fragments, like this:

ClassA mClassAFragment = ???;

I've tried using getSupportFragmentManager().findFragmentByTag() and I've also tried exploring the capabilities of mTabHost. Nothing can get them.

Can you suggest a way to do this or suggest an alternative approach?

Porcia answered 11/2, 2013 at 1:4 Comment(0)
P
11

OP here. To solve this problem I have overloaded the onAttachFragment method in my FragmentActivity:

public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);

    if (fragment.getClass() == ClassA.class) {
        ClassA mClassAFragment = (ClassA)fragment
            ...
        }
    }
}
Porcia answered 11/2, 2013 at 2:36 Comment(5)
Awesome advice. I think it's a pretty good approach to do it like this.Permanent
I think you should do a .equals() comparison for the fragment classes instead of a direct == comparator.Rohr
Or intanceof would be even better: 'fragment instanceof ClassA'Charterhouse
it will call only first time i hopeAstolat
How did you solve this problem please explain. I am using but it is calling first time after attached fragment never called againAstolat
R
3

You can get your fragment like this:

YourFragment frag = (YourFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.fragmentid));

To send data to a fragment you can follow this approach, creating a new transaction and sending the data through a bundle.

Bundle arguments = new Bundle();
arguments.putString("some id string", "your data");
YourFragment fragment = new YourFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().add(R.id.fragmentid, fragment).commit();
Ruckus answered 11/2, 2013 at 1:42 Comment(1)
Not bad. Unfortunately findFragmentById() doesn't work with FragmentTabHost because fragments are created only when they are the currently selected tab. I.e. ClassB and ClassC don't exist until I select them. I'll mark your response as the answer, because I think it's pretty close and it's very helpful. I'll provide my own answer for additional information.Porcia
Z
2

This can be accomplished by the 3rd argument of android.support.v4.app.FragmentTabHost.addTab(TabSpec, Class, Bundle args), then the args can be retrieved via android.support.v4.app.Fragment.getArguments()

public class Tab1Fragment extends BaseFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // do something with the arguments
    Log.i("DEBUG", "" + getArguments());
    // ...
}

}

Zonate answered 16/10, 2014 at 11:6 Comment(1)
Unfortunately, not. This was clearly the original intention but getArguments() always returns null there. A shame, really...Hush

© 2022 - 2024 — McMap. All rights reserved.