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.

[BottomSheetBehavior] State is incorrect if updated during settling animation

See original GitHub issue

Description:

BottomSheetBehavior#state is not updated correctly if it was updated during the BottomSheetBehavior.SETTLING state. BottomSheetCallback$onStateChanged is not called with the correct value either.

Visually, the sheet is in the proper state.

Expected behavior:

BottomSheetBehavior#state should match its visual state and BottomSheetCallback#onStateChanged should be called with the latest state change.

Source code:

Sample project: BottomSheetDemo.zip

Snippet to reproduce bug:

class BottomSheetView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) : FrameLayout(context, attrs, defStyleAttr, defStyleRes), CoordinatorLayout.AttachedBehavior {

    private val bottomSheetBehavior = BottomSheetBehavior<BottomSheetView>()

    init {
        // Initialize sheet to be hidden and set callback for when the state changes
        bottomSheetBehavior.isHideable = true
        bottomSheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(p0: View, p1: Float) = Unit

            override fun onStateChanged(p0: View, p1: Int) {
                println("state changed $p1")
            }

        })
        updateState(BottomSheetBehavior.STATE_HIDDEN)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()

        // Update state to be expanded (from hidden)
        postDelayed({
            updateState(BottomSheetBehavior.STATE_EXPANDED)
            // Update state to be hidden (from expanded during the settling animation)
            postDelayed({
                updateState(BottomSheetBehavior.STATE_HIDDEN)
                // Query the state of the sheet after animation completes
                postDelayed({
                    println("query state ${bottomSheetBehavior.state}")
                }, 1000)
            }, 300)
        }, 3000)
    }

    private fun updateState(state: Int) {
        println("update state to $state")
        bottomSheetBehavior.state = state
    }

    override fun getBehavior(): CoordinatorLayout.Behavior<BottomSheetView> = bottomSheetBehavior
}

On 1.1.0-alpha03 and newer versions (including 1.1.0-alpha09), the above outputs:

update state to 5
update state to 3
state changed 2
update state to 5
state changed 3
query state 3

On 1.0.0, the above outputs:

update state to 5
update state to 3
state changed 2
update state to 5
state changed 3
state changed 5
query state 5

The sheet’s end state should be HIDDEN (5), but it returns EXPANDED (3).

Android API version: Reproducible on API 21 (Lollipop) and 29 (Q Beta)

Material Library version: 1.1.0-alpha09

Device: Nexus 5X emulator and Pixel 3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
bwaldvogelcommented, Aug 31, 2020

I can still observe incorrect behavior with material-components version 1.2.0. The state change seems to be ignored when changing the state to BottomSheetBehavior.STATE_EXPANDED while the bottom sheet is currently settling.

The following workaround seems to solve the issue:

  private fun expandBottomSheet() {
+ 	if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_SETTLING) {
+ 		// workaround for https://github.com/material-components/material-components-android/issues/516
+ 		handler.removeCallbacksAndMessages(null)
+ 		handler.postDelayed({ expandBottomSheet() }, 10)
+ 	} else {
  		bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
+ 	}
  }
0reactions
vectercommented, Dec 6, 2019

Gracias @melaniegoetz, appreciate your help here!

Read more comments on GitHub >

github_iconTop Results From Across the Web

BottomSheetBehaviour setstate without animation
I have problems with the same, but in a bit different way - my UI state changes setHideable to false before that settling...
Read more >
BottomSheet doesn't expand correctly after setState ...
My current workaround (that costs me the initial animation from hidden to collapsed) is to not start hidden but with BottomSheetBehaviour.
Read more >
BottomSheetBehavior - Android Developers
Sets the height of the bottom sheet when it is collapsed while optionally animating between the old height and the new height.
Read more >
Handling Scrolls with CoordinatorLayout - CodePath Cliffnotes
CustomBottomSheetBehavior Sample - Demonstrates three-state phase shifts during scrolling of the bottom sheet. Refer to related stackoverflow post for ...
Read more >
BottomSheetBehavior.java - Google Git
@param slideOffset The new offset of this bottom sheet within [-1,1] range. Offset ... if (ss.state == STATE_DRAGGING || ss.state == STATE_SETTLING) {....
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