customAnimation when calling popBackStack on a FragmentManager

只谈情不闲聊 提交于 2020-12-27 17:05:44

问题


In my activity, with the touch of a button, I replace the current fragment with a new fragment using a custom animation, like in this example.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_anomalie:
            Fragment contentFragment = getFragmentManager().findFragmentById(R.id.content);

            if(contentFragment instanceof AnomalieListFragment)
            {
                getFragmentManager().popBackStack();
                return true;
            }
            else
            {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
                anomalieFragment = new AnomalieListFragment();
                ft.replace(R.id.content, anomalieFragment);
                ft.addToBackStack(null);
                ft.commit();
            }

    ...

However, popping back the stack doesn't show any animation. Is there a way to specify a custom animation like we do in a FragmentTransaction with the setCustomAnimations method?


回答1:


After further reading of the documentation, I found that using this signature of setCustomAnimation allowed the animation to be played when pressing the back button or calling getFragmentManager().popBackStack();

I modified my code like this

...
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out);
anomalieFragment = new AnomalieListFragment();
ft.replace(R.id.content, anomalieFragment);
ft.addToBackStack(null);
ft.commit();
...


来源:https://stackoverflow.com/questions/25550643/customanimation-when-calling-popbackstack-on-a-fragmentmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!