TabHost not working upon multiple Tab switches

125 views Asked by At

i have made a Master/Detail View which contains a TabHost in the Detailview. i have to populate a list of tabs, and every tab has a listview as its content.

Now when i change my Item to display, after the third change or so, no more tab content is rendered(The indicators are rendered). But when i change the tab, the content gets rendered. The onCreate Method of the DetailFragment is invoked when i change my item to display. The tabs are genrated and added to the tabhost. But after a few times i switched the item to display, the onCreateView Method of the OrderGroupTabFragment is not invoked anymore, without any reason to me. Thankful for any help.

Here is my code:
DetailFragment:

/**
 * A fragment representing a single Order detail screen. This fragment is either
 * contained in a {@link OrderListActivity} in two-pane mode (on tablets) or a
 * {@link OrderDetailActivity} on handsets.
 */
public class OrderDetailFragment extends Fragment implements IASyncMethodCallbacks {

private static Logger _logger = Logger.getLogger(OrderDetailFragment.class.getSimpleName());
/**
 * The fragment argument representing the item ID that this fragment
 * represents.
 * 
 */
public static final String ARG_ORDER = "order";

private TrafficSafetyOrder _order;
private TrafficSafetyOrderService _trafficSafetyOrderService;
private boolean _detached;
private FragmentTabHost tabHost;


/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public OrderDetailFragment() {
}

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

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    _detached = false;
    _trafficSafetyOrderService = new TrafficSafetyOrderService();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_order_detail,
            container, false);

    tabHost = (FragmentTabHost)rootView.findViewById(R.id.order_detail_tab_host);

    tabHost.setup(getActivity(), ((FragmentActivity)getActivity()).getSupportFragmentManager() , android.R.id.tabcontent);
    OrderListItem order = (OrderListItem)getArguments().getSerializable(ARG_ORDER);

    try {
        if(order != null)
            _order = _trafficSafetyOrderService.getOrderForId(order.getId());
    } catch (Exception e) {
        _logger.log(Level.WARNING, e.getMessage(), e);
    }

    if (_order != null) {
        OrderId orderId = _order.getOrderIdByOrderIdType(OrderIdTypes.GEB);
        ((TextView) rootView.findViewById(R.id.order_detail_header))
                .setText("Auftrag: " + orderId.getName() + orderId.getId());

        for (final Group formGroup : _order.getFormGroups()) {
            System.out.println("Adding Tab " + formGroup.getDisplayText());
            TabSpec spec = tabHost.newTabSpec(formGroup.getDisplayText());
            System.out.println("Spec created " + formGroup.getDisplayText());
            spec.setIndicator(formGroup.getDisplayText());
            System.out.println("Indicator set " + formGroup.getDisplayText());
            Bundle bundle = new Bundle();
            System.out.println("Bundle created " + formGroup.getDisplayText());
            bundle.putSerializable(OrderGroupTabFragment.ORDER_GROUP_LIST_ARG, formGroup.getItems());
            System.out.println("Added items to bundle " + formGroup.getDisplayText());
            tabHost.addTab(spec, OrderGroupTabFragment.class, bundle);
            System.out.println("Tab added " + formGroup.getDisplayText());
        }

    }
    return rootView;
}
@Override
public void onStart() {
    super.onStart();
    tabHost.setCurrentTab(0);
}
public TrafficSafetyOrder getOrder() {
    return _order;
}

public void setOrder(TrafficSafetyOrder _order) {
    this._order = _order;
}


@Override
public void onDestroyView() {
    super.onDestroyView();

    try {
        _trafficSafetyOrderService.updateTrafficServiceOrder(_order);
    } catch (Exception e) {
        _logger.log(Level.WARNING, e.getMessage(), e);
    }
}

@Override
public void onDetach() {
    super.onDetach();
    _detached = true;
}

@Override
public void onASyncMethodCompleted(Object receiver, Method method) {
    if(_detached)// if fragment is already detached from Activity, do not execute callback
        return;

    Toast.makeText(getActivity(), receiver != null ? receiver.getClass().getSimpleName() : "NULL" + "." + method != null ? method.getName() : "NULL" + " executed succesfully", Toast.LENGTH_LONG).show();
}
}

OrderGroupTabFragment:

public class OrderGroupTabFragment extends android.support.v4.app.Fragment{

    public static final String ORDER_GROUP_LIST_ARG = "ORDER_GROUP_ITEMS";
    private List<GroupItem> groupItems;
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
        System.out.println("Creating Order List View");
        View view = inflater.inflate(R.layout.fragment_order_group_tab_list, null);
        groupItems = (List<GroupItem>)getArguments().getSerializable(ORDER_GROUP_LIST_ARG);
        listView = (ListView)view.findViewById(R.id.order_detail_tab_list_view);
        listView.setAdapter(new GroupItemAdapter(getActivity(), groupItems));
        System.out.println("Order List View Created");
        return view;
}
}

GroupItemAdapter:

public class GroupItemAdapter extends ArrayAdapter<GroupItem> {

private Context _context;


public GroupItemAdapter(Context context, List<GroupItem> groupItems) {
    super(context, R.layout.lvi_group_item , groupItems);
    this._context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if(convertView == null){
        LayoutInflater li = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.lvi_group_item,
                parent, false);
        holder = new ViewHolder();
        holder.header = (TextView)convertView.findViewById(R.id.tab_header);
        holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    GroupItem item = getItem(position);

    holder.header = (TextView)convertView.findViewById(R.id.tab_header);
    holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);



    if(item != null)
        holder.header.setText(item.getName());

    return convertView;
}

private class ViewHolder {
    TextView header;
    CheckBox isVerified;
}

}

fragment_order_detail.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout android:id="@+id/order_detail_button_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    style="@android:style/Widget.ActionBar">

    <Button android:id="@+id/order_detail_button_undo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ActionButton"
        android:drawableTop="@drawable/ic_action_undo"
        android:text="Undo"
        android:layout_weight="1"/>
    <Button android:id="@+id/order_detail_button_redo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ActionButton"
        android:drawableTop="@drawable/ic_action_redo"
        android:text="Redo"
        android:layout_weight="1"/>
    <Button android:id="@+id/order_detail_button_submit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ActionButton"
        android:drawableTop="@drawable/ic_action_tick"
        android:text="Submit"
        android:layout_weight="1"/>
</LinearLayout>


<TextView
    style="?android:attr/textAppearanceLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:layout_below="@id/order_detail_button_bar"
    android:layout_alignParentStart="true"
    android:id="@+id/order_detail_header"/>

<android.support.v4.app.FragmentTabHost android:id="@+id/order_detail_tab_host"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/order_detail_header">
    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@android:id/tabs"/>

    </RelativeLayout>
</android.support.v4.app.FragmentTabHost>

fragment_order_group_tab_list.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/order_detail_tab_list_view"/>
</RelativeLayout>

lvi_group_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView android:id="@+id/tab_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"/>

<CheckBox android:id="@+id/tab_verification_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/tab_header"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@id/tab_header"
    android:text="@string/lvi_group_item_verification_succesfull"
    />

</RelativeLayout>

gr33tz gangfish

1

There are 1 answers

0
gangfish On BEST ANSWER

Got it working.

I took the wrong fragmentmanager to pass to the tabhost.setup() method in my OnCreateView method in the DetailFragment.

tabHost.setup(getActivity(), getChildFragmentManager() , android.R.id.tabcontent);