Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active February 9, 2018 08:27
Show Gist options
  • Save daichan4649/6462232 to your computer and use it in GitHub Desktop.
Save daichan4649/6462232 to your computer and use it in GitHub Desktop.
Fragment へBACK押下時イベントを通知する (Android)
public interface BackPressedListener {
void onBackPressed();
}
// Activity
@Override
public void onBackPressed() {
Fragment fragment = getFragmentManager().findFragmentByTag("");
if (fragment != null && fragment instanceof BackPressedListener) {
((BackPressedListener) fragment).onBackPressed();
} else {
super.onBackPressed();
}
}
// Fragment (implements BackPressedListener)
@Override
public void onBackPressed() {
//TODO BACK押下時処理
}
@pishguy
Copy link

pishguy commented Jul 1, 2016

Thanks. your code doesn't work on nested fragment sir. can you help me for this quastion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment