Skip to content

Instantly share code, notes, and snippets.

@prbale
Last active April 22, 2018 03:23
Show Gist options
  • Save prbale/37b5dc469f61ad3fe2ad14145de63045 to your computer and use it in GitHub Desktop.
Save prbale/37b5dc469f61ad3fe2ad14145de63045 to your computer and use it in GitHub Desktop.
Handle Back Press in Fragments
protected OnBackPressedListener onBackPressedListener;
//...
public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
this.onBackPressedListener = onBackPressedListener;
}
//...
@Override
public void onBackPressed() {
if (onBackPressedListener != null)
onBackPressedListener.doBack();
else
super.onBackPressed();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
activity = getActivity();
((BaseActivity)activity).setOnBackPressedListener(new BaseBackPressedListener(activity));
View view = ... ;
//stuff with view
return view;
}
public interface OnBackPressedListener {
public void doBack();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment