I want to disable the back button in a fragment class. onBackPressed()
doesn\'t seem to work in this fragment. How could I disable the back button?
This
Change
public void onBackPressed() {
}
to
@Override
public void onBackPressed() {
super.onBackPressed()
}
OR
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
return;
}
In your parent Activity
@Override
public void onBackPressed() {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.content_frame);
if (f instanceof yourfragment) {//the fragment on which you want to handle your back press
Log.i("BACK PRESSED", "BACK PRESSED");
}else{
super.onBackPressed();
}
}