I have two EditText. First, use to open DatePicker, and the second use to open TimePicker. I have created DialogFragment for two-component. Both works.All in the Fragment.
This is my Fragment. When select DatePicker the value of Time disappears and When I select TimePicker Date also disappear. I have already saveInstance. Pls, help!! Thx
DialogFragment datePicker;
TimePickerFragment timePicker;
EditText departureEdt;
EditText departureTimeEdt;
String selectedDate;
String selectedTime;
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (outState != null) {
outState.putString("selectedDate", selectedDate);
outState.putString("selectedTime", selectedTime);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
selectedDate = savedInstanceState.getString("selectedDate");
selectedTime = savedInstanceState.getString("selectedTime");
departureEdt.setText(selectedDate);
departureTimeEdt.setText(selectedTime);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final FragmentManager fm = ((AppCompatActivity) getActivity()).getSupportFragmentManager();
View view = inflater.inflate(R.layout.transportation_fragment, container, false);
departureEdt = view.findViewById(R.id.departure_date);
departureEdt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePicker = new DatePickerFragment();
datePicker.setTargetFragment(TransportationFragment.this, 1);
datePicker.show(fm, "datePicker");
}
});
departureTimeEdt = view.findViewById(R.id.departure_time);
departureTimeEdt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timePicker = new TimePickerFragment();
timePicker.setTargetFragment(TransportationFragment.this, 1);
timePicker.show(fm, "timePicker");
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// get date from string
selectedDate = data.getStringExtra("selectedDate");
selectedTime = data.getStringExtra("selectedTime");
// set the value of the editText
departureEdt.setText(selectedDate);
departureTimeEdt.setText(selectedTime);
}
}
}