florent37 / SingleDateAndTimePicker

You can now select a date and a time with only one widget !
Apache License 2.0
1.02k stars 334 forks source link

Window Leak on screen rotation #53

Open mg931 opened 7 years ago

mg931 commented 7 years ago

Hi,

Thanks for putting together this useful library.

Unfortunately I can't seem to avoid a WindowLeaked error when the picker is opened and I rotate the screen.

I am creating and showing the picker like this:

    dateAndTimePicker = new SingleDateAndTimePickerDialog.Builder(this)
                    .curved()
                    .minutesStep(1)
                    .backgroundColor(Color.WHITE)
                    .mainColor(Color.parseColor("#1A237E"))
                    .title("Test")
                    .listener(this).build();

            dateAndTimePicker.display();

Then in the onPause() method of my activity I call this:

if (dateAndTimePicker != null && dateAndTimePicker.isDisplaying()) { dateAndTimePicker.close(); }

Even when the close() method is executed I get the following exception when screen is rotated:

android.view.WindowLeaked: Activity com.otrathena.athena_otr.MainActivity has leaked window android.widget.FrameLayout{34ced2b V.E...C.. ........ 0,0-1080,1920 #7f0e009d app:id/bottom_sheet_background} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:386) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at com.github.florent37.singledateandtimepicker.dialog.BottomSheetHelper$1.run(BottomSheetHelper.java:62) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:168) at android.app.ActivityThread.main(ActivityThread.java:5845) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

Any help on how I can close the picker on rotation and avoid this error would be greatly appreciated.

Many thanks

b45h-de commented 7 years ago

I just looked into this problem.
It seems as if the problems lies within the hide() procedure in BottomSheetHelper.java. Normaly remove() in BottomSheetHelper.java should be called when closing the dialog. But it isn't. The reason is the handler/runnable in hide() that delays this call for 200ms. This is to late and so remove never gets called to remove the view and then this exception occurs. This problem could be solved by calling remove() outside this handler/runnable. But this would also mean that the animation to hide/close the dialog doesn't animate. Another solution would be to set the postDelayed timer to 100ms. The Animation is still there and the view gets removed in time. But it is questionable if this is a good solution and I don't think it might work on all devices.

@florent37 would do you think would be the best solution?