I have a number of EditText lines on a UI where the user enters some data. The user enters a Date (from a DatePicker fragment) on the fListenerEditText line. The focus is then returned to the first EditText line which is cListenerEditText. For the default Back button behavior, if the user then presses the Back button, the Activity would close and the Date data would be immediately lost, and the user is returned to the previous Activity.
In my case, I would like to launch a Dialog fragment that asks the user if they want to discard the Date they previously entered at fListenerEditText. If the user clicks the "OK" button, the data is discarded, the Activity closes and the user returns to the previous activity. Note in my case, the soft keyboard is not open when the user presses the Back button key.
How do I use a FocusListener and a Back button listener together? What am I missing here?
public class EditActivity extends AppCompatActivity {
private ListenerEditText cListenerEditText, dListenerEditText, eListenerEditText, fListenerEditText;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_edit);
cListenerEditText = (ListenerEditText) findViewById(id.CEditText);
dListenerEditText = (ListenerEditText) findViewById(id.DEditText);
eListenerEditText = (ListenerEditText) findViewById(id.EEditText);
fListenerEditText = (ListenerEditText) findViewById(id.FEditText);
final int stringDueDate = fListenerEditText.getText().toString().replace(" ", "").length();
cListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && fListenerEditText.getText().length() > 0) {
// add some type of Back button listener here.
}
}
});