I have two fragments [A] and [B].
In fragment [A] , I want to ignore back press so I have the following code
View view = inflater.inflate(R.layout.fragment_confirmation, container, false);
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
This works fine on fragment [A]. When i add fragment [B] to backstack, seems fragment [B] cannot handle back press although, the logic to ignore back press was only in fragment[A] . Can anyone explain this strange behaviour?
try this may be it works.Override the back key in onResume ...
@Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
// handle back button
return true;
}
return false;
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.