MaterialDatePicker callbacks lost on orientation change
See original GitHub issueHi, due to Androids “weird” handling of rotation changes the callbacks of the MaterialDatePicker are “lost” after orientation change.
Because the MaterialDatePicker ist final there is no way to extend it and override it and handle the click in my own code. A good way would be to change this:
confirmButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
for (MaterialPickerOnPositiveButtonClickListener<? super S> listener :
onPositiveButtonClickListeners) {
listener.onPositiveButtonClick(getSelection());
}
dismiss();
}
});
to sth like
confirmButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
onOkClick();
}
});
and add a function
public void onOkClick(){
for (MaterialPickerOnPositiveButtonClickListener<? super S> listener :
onPositiveButtonClickListeners) {
listener.onPositiveButtonClick(getSelection());
}
dismiss();
}
And make MaterialDatePicker NOT final
this way i could extend it and:
@Override
public void onOkClick(){
//save new date to my database
super.onOkClick();
}
I’m pretty sure that’s a much more elegant solution than either
- disable orientation changes while dialog is opened
- check in my activity after orientation change if the dialog is opened and reattach my callback
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Callback after orientation change becomes null - Stack Overflow
When configuration of the Activity being destroy and recreates. When Configuration of the phone is changed the method onConfigurationChange called.
Read more >MaterialDateTimePicker: Pick a date or time on Android in style
Why are my callbacks lost when the device changes orientation? Potential Improvements; License. Material DateTime Picker - Select a time/date in style. Join...
Read more >Handling Orientation Changes in Android - Medium
Proper handling of orientation changes makes rich user experience (not lost UI state) for the application and it also avoiding memory leaks.
Read more >Takafumi Kanda on Twitter: "Fragmentにリスナー登録したいときは ...
Hi, due to Androids "weird" handling of rotation changes the callbacks of the MaterialDatePicker are "lost" after orientation change.
Read more >20221688 Android-智慧型手機整理開箱評比,精選在Youtube上的開 ...
material-components / material-components-android Public ... MaterialDatePicker callbacks lost on orientation change #1688.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We cannot serialize the listeners to retain them.
Since the dialog is going to be recreated on a config change, you can find the fragment by the tag set when calling show.
Just reset the listeners in onResume or when appropriate:
@Nailik does it resolve the lost listeners issue after a config change like device rotation?