When the EditText line in the user interface receives focus, DatePickerFragment starts the date entry for the user.
When changing the orientation, if the EditText line has focus and the previously entered date (so that length() > 0 ), I donβt want the fragment to be shown automatically.
Is there a way to change or add code so that if the activity has been re-created and the EditText line is focused, it will not automatically launch DatePickerFragment ?
Would it be nice to do something in onResume() ?
Or do something with savedInstanceState() and !=null ?
public class Activity extends AppCompatActivity { ... EditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { ... if (hasFocus && (EditText.getText().length() == 0)) { DatePickerFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } else if (hasFocus && (EditText.getText().length() > 0)) { ... DatePickerFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } } }); }
source share