Reputation: 3144
I have a ViewPager
with some fragments where the user enters different inputs. One of these fragments contains a DatePicker
and a TimePicker
which I store (when user has selected a date and time) as a Joda LocalDate
and LocalTime
.
I want to save date and time por the sake of keep that input even the user is swiping between fragments. Here is my code:
@Override
public void onSaveInstanceState(Bundle outState) {
if (selectedDate != null && selectedTime != null) {
outState.putString("date", selectedDate.toString());
outState.putString("time", selectedTime.toString());
super.onSaveInstanceState(outState);
}
}
And in my onCreateView
:
if (savedInstanceState != null){
Log.d("mylog", savedInstanceState.getString("date");
currentDate = LocalDate.parse(savedInstanceState.getString("date"));
currentTime = LocalTime.parse(savedInstanceState.getString("time"));
selectedDateTv.setText(currentDate.toString("dd/MM/yyyy"));
selectedTimeTv.setText(currentTime.toString("HH:mm"));
} else {
//My code
}
The problem I'm facing is, if the user get to this fragment but doesn't enter any input, then swipe back to previous fragments and forth to the selecting date and time fragment, app crashes on my Log
line at onCreateView
.
I don't understand why if I try to avoid to save data if my selectedDate and selectedTime are null, when the fragment is recreated, bundle is different from null (and of course the key-value pairs don't exist).
Can you give me an approach on how to solve this? I would appreciate any explanation of this behaviour.
Upvotes: 1
Views: 1265
Reputation: 13009
The Bundle
is not null
after a configuration change because your Fragment
implementation is not the only one to use it. The following quote is from the documentation for Activity
but it is valid for Fragment
as well, as stated in Handling Runtime Changes:
However, even if you do nothing and do not implement onSaveInstanceState(), some of the activity state is restored by the Activity class's default implementation of onSaveInstanceState(). Specifically, the default implementation calls the corresponding onSaveInstanceState() method for every View in the layout, which allows each view to provide information about itself that should be saved.
So in addition to making sure the Bundle
is not null
, you should check if the extras with keys "date" and "time" are there at all. As you are saving both keys or none, it is sufficient to check for one of them:
if (savedInstanceState != null
&& savedInstanceState.containsKey("date")
){
Log.d("mylog", savedInstanceState.getString("date");
currentDate = LocalDate.parse(savedInstanceState.getString("date"));
currentTime = LocalTime.parse(savedInstanceState.getString("time"));
selectedDateTv.setText(currentDate.toString("dd/MM/yyyy"));
selectedTimeTv.setText(currentTime.toString("HH:mm"));
}
else {
//My code
}
Upvotes: 3
Reputation: 8835
change your method like below
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (selectedDate != null && selectedTime != null) {
outState.putString("date", selectedDate.toString());
outState.putString("time", selectedTime.toString());
}
}
Upvotes: 0