The answer to this is that you first @Override
the onTextChanged()
in fromLocation.addTextChangedListener
and then you @Override
it once more inside of toLocation.addTextChangedListener
. Then in the onCreate
inside of if (savedInstanceState != null) {
you have:
fromLocation.setText(savedInstanceState
.getCharSequence(FROM_LOCATION_TEXT));
toLocation.setText(savedInstanceState
.getCharSequence(TO_LOCATION_TEXT));
Which triggers both mentioned addTextChangedListener
s.
EDIT (following the update of the question):
In the onCreate
you call this, before any if
statements:
// Set EditTexts listeners
setFromLocationEditTextListeres();
setToLocationEditTextListeres();
Then you have:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.e(TAG, "onSaveInstanceState called.");
// Save activity state
savedInstanceState.putCharSequence(FROM_LOCATION_TEXT,
fromLocation.getText());
savedInstanceState.putCharSequence(TO_LOCATION_TEXT,
toLocation.getText());
super.onSaveInstanceState(savedInstanceState);
}
So, you're restoring the text in the EditTexts from here above, then in the onCreate
you set the listeners - thus setting the text on the EditTexts
triggers the onTextChanged
for bothe the EditTexts
. Afterwards in the onCreate
method you have:
if (savedInstanceState != null) {
Log.d(TAG, "Restoring state from saved bundle");
// Restore value of members from saved state
.../*Intentionnaly missed part*/
fromLocation.setText(savedInstanceState
.getCharSequence(FROM_LOCATION_TEXT)); <- Here you set the text again and trigger the `onTextChanged`
.../*Intentionnaly missed part*/
toLocation.setText(savedInstanceState
.getCharSequence(TO_LOCATION_TEXT)); <- Here you set the text again and trigger the `onTextChanged
} else {
Log.e(TAG, "Setting default values");
.../*Intentionnaly missed part*/
fromLocation.setText("Hello");<- Here you set the text again and trigger the `onTextChanged
}
I think that the described behaviour (e.g. "TextWatcher works fine until my activity is destroyed and before restoring from old saved bundle. But when I restore from saved bundle onTextChanged() is called for both of my EditTexts. This happens even if I don't restore their values (don't call setText()). Note that user is not interacting in any way.") happens only in the case of fromLocation
(EditText
) and not in both the cases (e.g. fromLocation
and toLocation
).
To finish this - you have also lots and lots of flags, which I don't even know what are they used for. To underline also the function editTextGainedFocus(EditText)
which is not described here.
Cheers.