20

I have an activity which holds and shows multiple fragments.

When i re-enter the fragment it auto fills text in all the editTexts. The same text for all fields as well.

Example:

Open fragment and fill in text in the two editTexts:
CustomEditText1: [______]
CustomEditText2: [_acb__]
CustomEditText3: [_qwe__]

Click back button and re-enter the fragment
CustomEditText1: [_qwe__]
CustomEditText2: [_qwe__]
CustomEditText3: [_qwe__]

This is my overwritten methods in the fragment:

public AddBookingFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    tabsActivity = (TabsActivity) getActivity();
}

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

    lastNameEditText = (NASEditText) view.findViewById(R.id.nas_add_booking_last_name);
    pnrEditText = (NASEditText) view.findViewById(R.id.nas_add_booking_pnr);

    addButton = (NASButton) view.findViewById(R.id.nas_add_booking_add_button);
    scanButton = (NASButton) view.findViewById(R.id.nas_add_booking_scan_button);

    confirmationBox = (LinearLayout) view.findViewById(R.id.nas_add_booking_confirmation_box);
    confirmationText = (NASHeaderAndSubtext) view.findViewById(R.id.nas_add_booking_confirmation_text);
    confirmationBox.setVisibility(View.GONE);

    bindButtons();

    FontHelper.setFont(container, tabsActivity);
    return view;
}

By debugging I can see that the editText is setting the text by breakpointing inside the overrided OnTextChanged.

This is the stacktrace from that breakpoint: Stacktrace
(NASEditText is my custom view)

Two problems / questions:

  1. How can I prevent the activity/fragment/editText from filling in the text in the fields when fragment is restored?
  2. Why is it filling in the same text for all fields?
Otziii
  • 2,304
  • 1
  • 25
  • 34
  • Probably you restore saved state somewhere. Show the code of your fragment – Sergey Glotov Apr 04 '17 at 13:36
  • @SergeyGlotov Look at the code. Im not restoring anything. Android is automatically saving the state of editTexts for fragments added to backstack afaik... – Otziii Apr 04 '17 at 13:42
  • 2
    @SergeyGlotov Android restores the state of EditText separately, a hacky solution to the problem is to subclass EditText and overwrite the onRestoreSavedState method, using setText("") there. But again, this is a hacky solution – Slugge Apr 04 '17 at 13:51
  • @Slugge why do you call that a hacky solution? – Peter Chaula Apr 05 '17 at 07:22
  • 1
    @peter because having it first set the text and then set it again to reset it is two actions that is uneccesary, it would be better to prevent the state from saving. Or alter the saved state – Slugge Apr 05 '17 at 07:24
  • See my last answer for the correct (not hacky) way to do this. :) – Otziii Apr 05 '17 at 10:05

2 Answers2

36

Found the problem with help from a friend!

SaveEnabled is default true on editTexts in Android.


The solution is simply to set:
setSaveEnabled(false);
on your editText.

Turns out Android isn't very smart in how it restores state for views with identical IDs in the same hierarchy (even in parent views with different IDs). I just turned this off because I save my state in a ViewModel object and restore it from my fragment's onCreateView().

Otziii
  • 2,304
  • 1
  • 25
  • 34
  • 1
    Had this issue in Xamarin and it took me forever to figure out what was happening because my callstack was worthless. Thanks so much for the answer! – rexar5 Sep 29 '18 at 00:57
  • 1
    "This link explains how to make it do it right if you want it to": the link is died, I want to handle it right without using ViewModel, any help please? – Phong Nguyen Jan 13 '20 at 09:28
3

This is not the answer, but it fixes the problem:

As commented by @Slugge:
Override the onSaveInstanceState method in your custom editText and clear the text / do what you want from there.

Otziii
  • 2,304
  • 1
  • 25
  • 34