How to prevent editText from auto filling text when fragment is restored from saved instance?
M

3

21

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?
Malefaction answered 4/4, 2017 at 13:29 Comment(6)
Probably you restore saved state somewhere. Show the code of your fragmentHumph
@SergeyGlotov Look at the code. Im not restoring anything. Android is automatically saving the state of editTexts for fragments added to backstack afaik...Malefaction
@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 solutionPhinney
@Phinney why do you call that a hacky solution?Bolan
@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 statePhinney
See my last answer for the correct (not hacky) way to do this. :)Malefaction
M
38

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

Malefaction answered 5/4, 2017 at 6:56 Comment(2)
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!Inhuman
"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?Natatorial
M
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.

Malefaction answered 4/4, 2017 at 13:53 Comment(0)
D
0

In addition to the accepted answer would like to highlight that this setting setSaveEnabled(false) can be done from layout too. Basically the same thing, but saves space in your .java / .kt files:

<EditText
        ...
        android:saveEnabled="false"
        ... />
Dumpy answered 11/4, 2024 at 1:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.