Skip to content

Instantly share code, notes, and snippets.

@Mojtaba-Shafaei
Last active September 22, 2020 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mojtaba-Shafaei/f66b8bcbc395fd07ac122b5bbfd11e8c to your computer and use it in GitHub Desktop.
Save Mojtaba-Shafaei/f66b8bcbc395fd07ac122b5bbfd11e8c to your computer and use it in GitHub Desktop.
Handle OnBackPressed in Fragments
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment