I added a BottomSheet to my application. I hide it on startup in onCreate method. Later on it's shown only when needed. The issue I encountered is when user exists the app (with the bottom sheet expanded) and later comes back to the app, the Activity is recreated and the bottom sheet stays on the screen - even if it set it to hidden in onCreate.
BottomSheetBehavior.from(myBottomSheet).state = BottomSheetBehavior.STATE_HIDDEN
The only solution I've found so far is to override onRestoreInstanceState callback, and don't let the Activity to restore it's state.
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
// FIXME Temporary fix for BottomSheet not hiding on app recreate
//super.onRestoreInstanceState(savedInstanceState)
}
I'm sure there must be a better solution. What may be causing this problem?
Layout definition:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/myBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:behavior_hideable="true"
app:behavior_peekHeight="90dp"
app:layout_behavior="@string/bottom_sheet_behavior">
</androidx.coordinatorlayout.widget.CoordinatorLayout>