2

TextWatcherアクティビティが破棄されるまで、保存された古いバンドルから復元する前に正常に動作します。しかし、保存したバンドルから復元するonTextChanged()と、両方の EditText に対して呼び出されます。これは、値を復元しなくても発生します (setText() を呼び出さないでください)。ユーザーは決して対話していないことに注意してください。

コード :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_trip);
    Log.d(TAG, "onCreate");

    fromLocation = (EditText) findViewById(R.id.from_location);
    toLocation = (EditText) findViewById(R.id.to_location);

    // Set EditTexts listeners
    setFromLocationEditTextListeres();
    setToLocationEditTextListeres();

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {

        Log.d(TAG, "Restoring state from saved bundle");
        ...

    } else {
        Log.e(TAG, "Setting default values");
        ... 
    }
}


// Set listener for fromLocation EditText
private void setFromLocationEditTextListeres() {

    fromLocation.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {

                Log.d(TAG, "fromLocation gained focus.");

            }
        }
    });

    fromLocation.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            Log.e(TAG, "Fromlocation after.");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if (fromLocation == null) {
                Log.d(TAG, "fromLocation is null................");
            }

            if (count != before) {
                editTextValueChanged(fromLocation);
            }
        }
    });

    fromLocation.setOnKeyListener(new View.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            // If user pressed enter
            if (keyCode == KeyEvent.KEYCODE_ENTER) {

            }

            // If pressed other than enter key then let others handle the
            // event
            return false;
        }
    });
}

// Set listener for toLocation EditText
private void setToLocationEditTextListeres() {

    toLocation.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {

                Log.d(TAG, "toLocation gained focus.");
            }
        }
    });

    toLocation.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            Log.e(TAG, "Tolocation after.");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if (count != before) {
                editTextValueChanged(toLocation);
            }
        }
    });

    toLocation.setOnKeyListener(new View.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            // If user pressed enter
            if (keyCode == KeyEvent.KEYCODE_ENTER) {

            }

            // If pressed other than enter key then let others handle the
            // event
            return false;
        }
    });

}

@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);
}
4

2 に答える 2