Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 385
Otziii Avatar asked Apr 04 '17 13:04

Otziii


People also ask

How to disable autofill in EditText Android?

Users can enable or disable autofill as well as change the autofill service by navigating to Settings > System > Languages & input > Advanced > Input assistance > Autofill service.

How to retain fragment state Android?

Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.

How to Save state fragment?

The first key is that we can save our Fragments' state ourselves using FragmentManager. saveInstanceState(Fragment) . Before removing the Fragment, call this method to get a Bundle containing your Fragment's saved state! The second key is restoring state.


1 Answers

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.

like image 141
Otziii Avatar answered Oct 23 '22 06:10

Otziii