要解决按下返回箭头或返回按钮时,片段退出动画不会触发的问题,可以尝试以下方法:
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_example, container, false);
// Set exit animation for fragment
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right)
.replace(R.id.fragment_container, new ExampleFragment())
.addToBackStack(null)
.commit();
return view;
}
请注意,上述示例代码中的R.anim.slide_in_left和R.anim.slide_out_right是自定义的动画资源,您可以根据自己的需求替换为其他动画资源。
使用上述方法,当按下返回箭头或返回按钮时,片段退出动画将会触发。