0

I've seen all the posts of TabHost + FragmentActivity on Google Groups and on StackOverflow and I'm still getting problems.

And I don't understand anything.

I'm following this tutorial for TabHost and FragmentActivity: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

I have the following thing: in a tab I load a CategoriaFragment.class, where I load a ListView and I set a ListView.OnItemClickListener for it. When I click, I call this code:

CategoriaFragment fragmentnuevo = new CategoriaFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
Bundle b = new Bundle();
b.putBoolean("flagSottoCategorie", true);
b.putSerializable("sottocategorielista", (Serializable)sottocategorielista);
fragmentnuevo.setArguments(b);
transaction.addToBackStack(null);
transaction.add(R.id.realtabcontent, fragmentnuevo, FragmentTAG); // FragmentTAG is the same tag that the current Fragment
transaction.commit();

and yes, I call the same type of Fragment because I need the same recurses but with another List(sottocategorielista). This is not the error because I've tried to use another .class and it's the same error.

When I press back button, I get again my first ListView but listeners are not available. So I click and nothing happens. I've used add and replace as well.

Moreover, when I try to change Tab, if add is typed, I get several fragments in the same layout, if remove is typed, I get only one fragment on onTabChanged, but when I get back to my Fragment, nothing is available. Moreover if I press back button, I get the java.IllegalStateException saying

Fragment is already added: CategoriaFragment

What should I do? Everything is easier with TabActivity, which is deprecated now and I would like to do everything with Fragments.

This is how my onTabChanged works (also available on the resource on the web)

public void onTabChanged(String tag) {
    TabInfo newTab = this.mapTabInfo.get(tag);
    if (mLastTab != newTab) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(this,
                        newTab.clss.getName(), newTab.args);
                ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
    }
}

Thanks in advanced.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • You can either remove the previous fragment and add the new one or just call fragmentTransaction.replace() and add your new fragment. – reixa Feb 06 '14 at 12:45
  • Fallow my post .. http://stackoverflow.com/questions/18120510/dynamically-changing-the-fragments-inside-a-fragment-tab-host/19859871#19859871 – AndroidHacker Feb 06 '14 at 12:46
  • FWIW, here is my book's action bar tabs sample app: https://github.com/commonsguy/cw-omnibus/tree/master/ActionBar/TabFragmentDemo – CommonsWare Feb 06 '14 at 12:47
  • @CommonsWare I'm not using the ActionBar, but a independent TabHost because I want to handle to the bottom – Rafael Ruiz Muñoz Feb 06 '14 at 12:54
  • Oops, sorry, I misread the method signature of your code snippet. Note that bottom tabs violate the Android design guidelines. Beyond that, try `FragmentTabHost` rather than `TabHost`. – CommonsWare Feb 06 '14 at 12:56
  • Yeah, I'd love to have the way to use my ActionBar at the bottom of my screen, but I looked for and I got anything :\ – Rafael Ruiz Muñoz Feb 06 '14 at 13:04
  • @AndroidHacker the same problems happen. If I go to my TAB2 where there's my `ListView` is, I switch to TAB3 and go back to TAB2, my `ListView` doesnt have listeners. – Rafael Ruiz Muñoz Feb 06 '14 at 13:09
  • @RafaFirenze you need to perform some check condition ...Fallow my answer for same. – AndroidHacker Feb 10 '14 at 04:05

1 Answers1

0

Some time there's problem for listeners in list view or more specifically I say some time our list view loads every time we switch our view in Fragment.

To solve this problem android in itself provides some methods which I believe can be found very easily over stack.

So I am going to put some general way to perform same.

(1) Define boolean value say Boolean androidHacker = false;

(2) Now when you populate your list view then set that value to true. i.e when you successfully fetch your value then set same to true .. androidHacker = true;

(3) Now every time on OnCreateView(); method check that value like ..

if(androidHacker == true){

// Simply populate your value using Model and Adapter

}else{

//fetch your server data  and inflate same to list view as this is first time your have //entered your fragment class 
}

Hope this solves your problem!

For more reference on using FragementTabHost fallow ..Dynamically changing the fragments inside a fragment tab host?

Community
  • 1
  • 1
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45