5

I've been searching for a while now but I think most of the reported bugs (and there are quite a few) in android.support.design.widget.TextInputLayout differ a little from this one. At least, I have solved most of the other bugs, but struggle with this one. I currently have a Fragment in my activity with a couple of TextInputLayout like 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 meeting some external condition (not important) I open and show another fragment (100% screen) which hides the aforementioned fragment. In case you wonder this new fragment asks for some extra fields that I need in that particular case. This is the code that handles the creation of the new Fragment:

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

BUT the problem is that, when going back (back button press, toolbar/action bar home button, etc..) to the first fragment. All of my TextInputLayouts lose the text that was inserted on them. This is really annoying and didn't happened when working exclusively with EditText, like we did previous our Material Design transition.

Moreover, this does not happen if instead of replacing fragments using a FragmentTransaction, I start a new Activity. Unfortunately, this is not what we really want. And we shouldn't need to do this kind of workaround.

Any ideas? Has this happened to anyone?

acrespo
  • 1,134
  • 1
  • 10
  • 33

1 Answers1

6

Posting my "answer", which is really a workaround and may not prove useful or satisfactory to everybody but at least it served me well.

This is clearly a bug with the new TextInputLayout, which does not handle very well the savedInstanceState when doing a replacement or removal in a FragmentTransaction. EditText did a really good job previously.

What I ended up doing is not using FragmentTransaction#replace() (because I traced the problem to the removal of the fragment), but instead using a combination of FragmentTransaction#hide() and FragmentTransaction#add(). This offers exactly the same visual effect and behaviour and does not has the problem of the mentioned bug. It only has the inherent drawback of, obviously, not removing the fragment: the fragment resources cannot be released/use for other purposes. This may cause trouble if memory is short or your fragment is a monstrosity. But at least in my case it didn't cause any trouble.

To sum up, this is what I finally use as my fragment transaction:

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();

Hope it can help some of you!

acrespo
  • 1,134
  • 1
  • 10
  • 33
  • I am losing all my Text Data during a orientation change. I am not even moving away from the fragment. Just an orientation change, and I lose all data. Has anyone faced this problem with `TextInputLayout`? Is there a solution to this? – Sajib Acharya Nov 09 '15 at 17:56
  • 2
    How is this working nowadays? I'm having the same issue but I'm using navigation. – groff07 Sep 21 '21 at 06:52