What Does Onbackpress Call In A Fragment
In a Fragment i have this: onPause(){ super.onPause(); if(flag){ getActivity.finish(); }else{ } } onResume(){ flag = true; super.onResume(); } and there is a Button, on click i
Solution 1:
If you want to close the activity on the back press, then Override the onBackPressed
inside the activity as following:
publicvoidonBackPressed() {
Fragmentfragment= getSupportFragmentManager().findFragmentByTag("yourfragment");
if (fragment instanceOf YourFragmet) {
finish();
return;
}
super.onBackPressed();
}
If you want to close the activity on the button click, then you can call
Button.onClick{
getActivity().onBackPressed();
}
OR
Button.onClick{
getActivity().finish();
}
If When you are transitioning between Fragments, you call addToBackStack() as part of your FragmentTransaction, then the back button will take you to the fragment on the top of the back stack
Solution 2:
Why can't you use onDetach() of Fragment? On Back key pressed, there will be onDetach() called on the fragment to remove that.
@Override
public void onDetach() {
super.onDetach();
if(flag){
getActivity.finish();
}else{
}
}
Post a Comment for "What Does Onbackpress Call In A Fragment"