Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to setCustomAnimations for FragmentTransaction on this code

i have 5 fragments and I use the following code to setCustomAnimations for FragmentTransaction:

FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

but how to setCustomAnimations for this one:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0)
        {
            this.finish();
            return false;
        }
        else
        {
            getSupportFragmentManager().popBackStack();
            return false;
        }



    }

    return super.onKeyDown(keyCode, event);
}
like image 340
ali-star Avatar asked Jul 29 '14 10:07

ali-star


People also ask

What is FragmentTransaction in android?

At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.

What is addToBackStack in android?

By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.

How do you animate a fragment?

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

How do you animate fragment transitions?

At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.


1 Answers

There is a second setCustomAnimations method (here's the support library method) that has two extra IDs present for the inclusion of the animation to be used when you pop the back stack. Pass the animation IDs you wish to occur when the back stack is popped (transaction is reversed) in the last to spots.

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, 
                                android.R.anim.slide_out_right,
                                ANIMATION_ID_FOR_ENTERING_VIEW,
                                ANIMATION_ID_FOR_EXITING_VIEW);

Then when you call

getFragmentManager().popBackStack();

The animations will play, the third animation passed to that method will play for the view that you originally removed, and the fourth will play for the view that was visible and is being removed. From the current animations you have for the initial transaction, I would guess you want to use android.R.anim.slide_in_right and android.R.anim.slide_out_left for the back stack animations (see below):

transaction.setCustomAnimations(android.R.anim.slide_in_left,
                                android.R.anim.slide_out_right,
                                android.R.anim.slide_in_right,
                                android.R.anim.slide_out_left);

You only have to call setCustomAnimations when initially adding the fragment to the stack (just like you are doing now, simply add the extra animation IDs), the back stack will remember the animations you set and will automatically play them when you pop back.

NOTE: This method is only available in API 13 and above unless you are using the support v4 jar, which it looks like you are (as you are using getSupportFragmentManager() instead of the regular method).

like image 141
anthonycr Avatar answered Oct 03 '22 03:10

anthonycr