question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MaterialDatePicker callbacks lost on orientation change

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
ymariancommented, Sep 8, 2020

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:

MaterialDatePicker datepicker  = (MaterialDatePicker) supportFragmentManager.findFragmentByTag("tag");
// reset listeners
0reactions
Rajarmlcommented, Nov 24, 2022

@Nailik does it resolve the lost listeners issue after a config change like device rotation?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found