Issue
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 is my sample code:
public class Login extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.login, null);
return root;
}
public void onBackPressed() {
}
}
Solution
You have to override onBackPressed of parent FragmentActivity class. Therefore, put your codes in parent FragmentActivity. Or you can call parent's method by using this:
public void callParentMethod(){
getActivity().onBackPressed();
}
in FragmentActivity override onBackPressed Method and not call its super class to disable back button.
@Override
public void onBackPressed() {
//super.onBackPressed();
//create a dialog to ask yes no question whether or not the user wants to exit
...
}
Answered By - Gunhan
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)