0

I want to slide up a fragment on its entry and slide down it on its exit. It should be pretty simple but I am facing issues.

Animation code : Slide Down - >

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_shortAnimTime"
    android:fromYDelta="100%p"
    android:toYDelta="0%p"
    android:interpolator="@android:anim/accelerate_interpolator" />

Slide Up ->

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_shortAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator" />

This is my code for fragment transition :

try {
            if (fragment != null && mSelectedTabIndex != NO_TAB) {
                FragmentTransaction ft = createTransactionWithOptions(transactionOptions);
                detachCurrentFragment(ft);
                    ft.setCustomAnimations(R.animator.slide_up,R.animator.slide_down,R.animator.slide_up,R.animator.slide_down);

                ft.add(mContainerId, fragment, generateTag(fragment));
                ft.commit();

                executePendingTransactions();
                mFragmentStacks.get(mSelectedTabIndex).push(fragment);

                mCurrentFrag = fragment;
                if (mTransactionListener != null) {
                    mTransactionListener.onFragmentTransaction(mCurrentFrag, TransactionType.PUSH);
                }

            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }

But I am not getting desired results. And I don't wish to replace the fragment but always add the fragment.

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62

1 Answers1

1

Try using this simple solution from the anim folder animations.

Replace this

fragmentTransaction.replace(R.id.frame, firstFragment, "fragment_class_name");

with

fragmentTransaction.add(R.id.frame, firstFragment, "fragment_class_name");

Example

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_up, R.anim.slide_up);
fragmentManager.addOnBackStackChangedListener(this);
fragmentTransaction.add(R.id.frame, firstFragment, "fragment_class_name");
fragmentTransaction.addToBackStack("fragment_class_name");
fragmentTransaction.commit();

Reason to use add fragment The important difference is:

replace removes the existing fragment and adds a new fragment..

but add retains the existing fragments and adds a new fragment that means existing fragment will be active and they wont be in 'paused' state hence when a back button is pressed onCreateView() is not called for the existing fragment(the fragment which was there before new fragment was added).

See the screenshot

enter image description here

Quick learner
  • 10,632
  • 4
  • 45
  • 55