Last active
April 22, 2018 03:23
-
-
Save prbale/37b5dc469f61ad3fe2ad14145de63045 to your computer and use it in GitHub Desktop.
Handle Back Press in Fragments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected OnBackPressedListener onBackPressedListener; | |
//... | |
public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) { | |
this.onBackPressedListener = onBackPressedListener; | |
} | |
//... | |
@Override | |
public void onBackPressed() { | |
if (onBackPressedListener != null) | |
onBackPressedListener.doBack(); | |
else | |
super.onBackPressed(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface OnBackPressedListener { | |
public void doBack(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment