On multiple fragment transaction, only last one is placed in the first container

48 Views Asked by At

I'm trying to place multiple graphs in a Fragment. So I have some FrameLayout container in a LinearLayout, in the main fragment.

When I make the transaction of all my GraphWidgetFragment in their container, the result I get is the last GraphWidgetFragment instance is in the container intented for first graph.

Also, all other containers are filled with default GraphWidgetFragment, but it seems that those ones don't even pass through onCreateView/onResume etc because I'm updating the TextView in it and the text displayed is the original one (set in xml).

I know all my graph instances are existing somewhere because I can access them and get values put at construction, but except the last one, others are not visible.

I replaced GraphFragment with a simple BlankFragment to make it easier to understand and debug.

  • I put onClickListener on FrameLayout containers, so I know that each container is at the right place.
  • I tryed to move transaction to onViewCreated() but same result. I tryed to do commit()/commitNow()/commitNowAllowingStateLoss() between all transaction, executePendingTransaction(), setReorderingAllowed(), still the same result as sending all in one commit.
  • Fragments are not replaced/superposed in same container, I tried using add() instead of replace() and set transparent colors.

Here is the "main Fragment" code : (onCreateView()

public class FMGraphsFragment extends Fragment {

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

        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5);

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.setReorderingAllowed(true);
            transaction.add(R.id.temp_graph_container, new BlankFragment(0), EXTTEMP.toString());
            transaction.add(R.id.ltft_graph_container, new BlankFragment(1), LTFT.toString());
            transaction.add(R.id.stft_graph_container, new BlankFragment(2), STFT.toString());
            transaction.add(R.id.lambda_graph_container, new BlankFragment(3), LAMBDA.toString());
            transaction.add(R.id.realtemp_graph_container, new BlankFragment(4), REALTEMP.toString());
            transaction.add(R.id.dutycycle_graph_container, new BlankFragment(5), DUTYCYCLE.toString());
            transaction.commit();
        } else {
            for(Integer a : list) {
                Fragment fragment = getChildFragmentManager().findFragmentByTag(LAMBDA.toString());
                if (fragment instanceof BlankFragment) {
                    ((BlankFragment)fragment).setA(a);
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.view_fragment_graphs, container, false);
    }
}

And my futur GraphWidgetFragment (aka Blank) :

public class BlankFragment extends Fragment {

    private int a;

    public BlankFragment() {
        // Required empty public constructor
        a = 999;
    }

    public BlankFragment(int b) {
        a = b;
    }

    public static BlankFragment newInstance(String param1, String param2) {
        return new BlankFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tv = (TextView) getActivity().findViewById(R.id.texttt);
        String s = "";
        for (int i = 0; i < a; i++) {
            s = s + " ";
        }
        s = s + String.valueOf(a);
        tv.setText(s);
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getA() {
        return a;
    }
}

The main fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin_fine"
    tools:context=".ui.graph.FMGraphsFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/temp_graph_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/graph_height"
            android:layout_margin="@dimen/margin_fine">
        </FrameLayout >

        <FrameLayout
            android:id="@+id/ltft_graph_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/graph_height"
            android:layout_margin="@dimen/margin_fine">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/stft_graph_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/graph_height"
            android:layout_margin="@dimen/margin_fine">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/lambda_graph_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/graph_height"
            android:layout_margin="@dimen/margin_fine">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/realtemp_graph_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/graph_height"
            android:layout_margin="@dimen/margin_fine">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/dutycycle_graph_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/graph_height"
            android:layout_margin="@dimen/margin_fine">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/dummy"
            android:layout_width="match_parent"
            android:layout_height="50dp">
        </FrameLayout>

    </LinearLayout>

</ScrollView>

Here is what I get: Phone screenshot

I don't know where to search next so... I rely on you

0

There are 0 best solutions below