I have a fragment with a bottom sheet inside and I want to hide it when back button is pressed, then with the second back go to previous fragment,I tried to use onBackPressed
but it doesn't work. setOnKeyListener()
it doesn't work too. Some help solution, wellcome.
Asked
Active
Viewed 3,657 times
0
1 Answers
0
this may help you !
@Override
public void onBackPressed() {
touchToBack++;
if (touchToBack== 1) {
if (bottomSheetBehavior!=null && bottomSheetBehavior.getState() !=
BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
touchToBack= 0;
return;
}
}
if (touchToBack== 1 || touchToBack> 1) {
if (bottomSheetBehavior!=null && bottomSheetBehavior.getState() ==
BottomSheetBehavior.STATE_HIDDEN) {
touchToBack= 0;
super.onBackPressed();
}
}
}

mohammadReza Abiri
- 1,759
- 1
- 9
- 20
-
2I can not use onBackPressed() directly on fragment, and Main Activity can not see BottomSheetBehavior that is in my fragment – young1872 May 16 '19 at 13:33
-
make it static! and make sure it is not empty! – mohammadReza Abiri May 16 '19 at 13:41
-
I want a fragment where i can edit a profile image, when I click on a edit imageview it opens the bottomsheet with options of access to gallery and delete image. Everything works good, the problem is when I want to go back and hide the bottomsheet before and then to go back to the previous fragment. – young1872 May 16 '19 at 13:52
-
see this link https://stackoverflow.com/questions/5448653/how-to-implement-onbackpressed-in-fragments – mohammadReza Abiri May 16 '19 at 16:22