android – Transition animation between fragments depends on position in stack-ThrowExceptions

Exception or error:

I’m using a BottonNavigationView in my app which switches between 4 fragments. since all fragments must be kept in memory and must not be created each time selected, they are created and added to a container at start, and then depending on which item is selected, all fragments are hided and the corresponding fragment is showed.

fragment1= Fragment1.newInstance();
    fragment2= Fragment2.newInstance();
    fragment3= Fragment3.newInstance();
    fragment4= Fragment4.newInstance();

    getSupportFragmentManager().beginTransaction().add(R.id.main_fragments_holder, fragment1).hide(fragment1).commit();
    getSupportFragmentManager().beginTransaction().add(R.id.main_fragments_holder, fragment2).hide(fragment2).commit();
    getSupportFragmentManager().beginTransaction().add(R.id.main_fragments_holder, fragment3).hide(fragment3).commit();
    getSupportFragmentManager().beginTransaction().add(R.id.main_fragments_holder, fragment4).commit();

    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.bottom_nav_1: {
                    getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_mild, R.anim.exit_mild).hide(fragment2).hide(fragment3).hide(fragment4).show(fragment1).commit();
                    break;
                }
                case R.id.bottom_nav_2: {
                    getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_mild, R.anim.exit_mild).hide(fragment1).hide(fragment3).hide(fragment4).show(fragment2).commit();
                    break;
                }
                case R.id.bottom_nav_3: {
                    getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_mild, R.anim.exit_mild).hide(fragment1).hide(fragment2).hide(fragment4).show(fragment3).commit();
                    break;
                }
                case R.id.bottom_nav_4: {
                    getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_mild, R.anim.exit_mild).hide(fragment1).hide(fragment2).hide(fragment3).show(fragment4).commit();
                    break;
                }
            }

            return true;
        }
    });

the problem is that since these fragments have different elevations (depending on their position in stack), the transition animation between them is not the same. if the current fragment is upper, the entrance animation of the selected fragment cannot be seen (because it is below the current fragment). how to handle this situation so that the transition animations are the same?

the following animations are used:

enter_mild.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fillAfter="false"
android:interpolator="@android:anim/decelerate_interpolator">
<alpha
    android:fromAlpha="0"
    android:toAlpha="1" />
<scale
    android:fromXScale=".9"
    android:fromYScale=".9"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1"
    android:toYScale="1" />
</set>

exit_mild.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fillAfter="false"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
    android:fromAlpha="1"
    android:toAlpha="0" />
<scale
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale=".9"
    android:toYScale=".9" />
</set>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *