BackStack in Fragments:-
Create Interface class:
public interface OnBackListner {void back();
}
In Activity:
public class HomeActivity extends AppCompatActivity{OnBackListner onBackListner;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigationdrawer);
}
@Override
public void onBackPressed() {
if (onBackListner != null) {
onBackListner.back();
} else {
finish();
}
}
public void setOnBackPressedListener(OnBackListner onBackPressedListener) {
this.onBackListner = onBackPressedListener;
}
}
In 1st Fragment :
((HomeActivity) getActivity()).setOnBackPressedListener(new OnBackListner() {@Override
public void back() {
//startActivity(new Intent(getActivity(), MainActivity.class));
CommonUtils.hideKeyboard(getActivity());
getActivity().finish();
}
});
2nd Fragment:
((HomeActivity) getActivity()).setOnBackPressedListener(new OnBackListner() {@Override
public void back() {
//startActivity(new Intent(getActivity(), MainActivity.class));
CommonUtils.hideKeyboard(getActivity());
getActivity().getSupportFragmentManager().popBackStackImmediate();
}
});
No comments:
Post a Comment