Android: TabHost - passing Parameters to Fragments
Asked Answered
F

1

10

I am working on a TabHost with a WebView inside of each Fragment. What I do is:

for(int i = 0; i < numberOfTabs; i++){
        mTabManager.addTab(mTabHost.newTabSpec(tabNames[i]).setIndicator(tabNames[i]),
                Web.class, null);
 }

How can I pass parameters to each Fragment. In this case I want to pass an URL to each Fragment inside the TabHost.

Thx in advance.

Fife answered 19/10, 2012 at 13:53 Comment(2)
Did you get this? I need the same. Thanks!Outfight
I am not sure. I think I implemented a static class which holds the data. Or something like that. Currently I am not working on this particular project anymore.Fife
O
22

Finally I get the solution. You can pass parameters using a Bundle in the last parameter of "addTab" where you have "null".

for(int i = 0; i < numberOfTabs; i++){
        Bundle b = new Bundle();
        b.put...
        mTabManager.addTab(mTabHost.newTabSpec(tabNames[i]).setIndicator(tabNames[i]),
                Web.class, b);
 }

And then in the Fragment you can get the Bundle with getArguments().

I hope it will be useful for someone in the future

Outfight answered 5/12, 2012 at 15:2 Comment(3)
but inside the fragment's onCreateView(), getArguments() always gives me null, do you know why?Acutance
Yo have to do it in the onCreateOutfight
Sorry, it was my bad, I forgot to pass it while instantiating the tab in my tabListener :)Acutance

© 2022 - 2024 — McMap. All rights reserved.