2

So I have a class that extends a Fragment, which in it, I have a FragmentTabHost, my FragmentTabHost consist of 3 Tab, each tab will show a list of item.. Inside each item I can update the item property like its name/ other property. My problem is after I update the value and pressed back button, my list of item is still consist the old list where my item value isn't updated. My list is updated only if I switch to another tab and go back again. I've been struggling for couple of hours with this problem, any help will be much appreciated Thanks

package com.example.hansel.tapbandung_v00.page.Menu_Parent;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.hansel.tapbandung_v00.R;
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_category;
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_featured;
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_newest;

/**
 * Created by Hansel on 5/25/2015.
 */
public class Business_parent extends Fragment{
private FragmentTabHost mTabHost;
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity().getApplicationContext();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    //3 tab category, dll
    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabhost);
    mTabHost.addTab(mTabHost.newTabSpec("featured").setIndicator("Featured"),
            bus_featured.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("category").setIndicator("Category"),
            bus_category.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("newest").setIndicator("Newest"),
            bus_newest.class, null);
    return mTabHost;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    mTabHost = null;
}

}

  • I have a similar query: I'd like to reload all the tab host upon item update. My list items can switch from one list to another depending on the update, so I need to update the whole tab host everytime an item is changed in one of the tabs. – Supercelo Feb 17 '16 at 11:54

1 Answers1

0

I've been struggling with the same problem, and finally I came up with such a solution:

mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab1Fragment()).commit();
            mTabHost.setCurrentTab(0);
        }
    });

    mTabHost.getTabWidget().getChildAt(1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab2Fragment()).commit();
            mTabHost.setCurrentTab(1);
        }
    });

    mTabHost.getTabWidget().getChildAt(2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab3Fragment()).commit();
            mTabHost.setCurrentTab(2);
        }
    });

It works. I'm not sure this is a very elegant solution though.

Kasia
  • 1
  • 1