Android TextInputLayouts will lose text / content when a fragment returns in a transaction

I have been looking for some time, but I think that most of the recorded errors (and quite a few) are android.support.design.widget.TextInputLayoutslightly different from this. At least I solved most of the other mistakes, but deal with it. I currently have Fragmentin my work with a couple TextInputLayoutlike this

<android.support.design.widget.TextInputLayout
    android:id="@+id/input1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint1"
        android:inputType="numberSigned" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/input2
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint2"
        android:inputType="numberSigned"/>
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/input3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint3"
        android:inputType="numberSigned">
</android.support.design.widget.TextInputLayout>

And, after you meet some external condition (it doesn’t matter), I open and show another fragment (100% screen) that hides the above fragment. If you're interested, this new snippet asks for some additional fields that I need in this particular case. This is the code that handles creating a new one Fragment:

Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction()
                                .replace(((ViewGroup) getView().getParent()).getId(), fragment2);
transaction.addToBackStack(Fragment1.class.getSimpleName());
transaction.commit();

, ( , / ..) . TextInputLayouts , . EditText, Material Design.

, , FragmentTransaction Activity. , , . .

? -?

+4
1

"", , , , .

TextInputLayout, savedInstanceState FragmentTransaction. EditText .

, , FragmentTransaction#replace() ( ), FragmentTransaction#hide() FragmentTransaction#add(). . , , : / . , . , , .

, , , , :

Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(((ViewGroup) getView().getParent()).getId(), fragment2);
transaction.hide(Fragment1.this);
transaction.addToBackStack(Fragment1.class.getSimpleName());
transaction.commit();

, !

+4

Source: https://habr.com/ru/post/1598136/


All Articles