Hassan Tariq
Hassan Tariq

Reputation: 63

What are lifecycle events of bottomsheetbehaviour?

I searched on the internet about the lifecycle events of bottomsheetbehaviour in android but could not find anything.I want to develop a thing same like facebook comment and like system in which if we click on comment then a bottom sheet appears and if we hit like on bottomsheet then when we close that bottomsheet the like is visible on the activity from which bottom sheet was started.I could not find a way for this.I tried to call onPause and onResume override method but these do not call.Any help will be appreciated.Thanks

Upvotes: 0

Views: 775

Answers (1)

Nikos Hidalgo
Nikos Hidalgo

Reputation: 3766

Create the BottomSheetBehaviour as a nested layout in your activity. When you create the activity, after you bind the views, initialise the behaviour:

bottomSheetBehavior = BottomSheetBehavior.from(yourNestedLayoutForTheBottomSheet);
bottomSheetBehavior.setPeekHeight(0);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

Then when you need the BottomSheet to appear simply use the following:

bottomSheetBehavior.setPeekHeight(300);

Where 300 is a peekHeight. You can adjust this to use any integer that works with your specific layout. You can also have a toggle option to show/hide the bottom sheet.

So for example on a click of a button:

if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else {
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}

EDIT: as the BottomSheetBehaviour is part of your activity's layout; and hence can access other views in the activity. So, you won't be messing with the lifecycle methods at all. Just add the appropriate clickListeners on your buttons and you're done!

Upvotes: 1

Related Questions