package com.mcxtzhang.animdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private CardFrontFragment mCardFrontFragment; private CardBackFragment mCardBackFragment; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mCardFrontFragment = new CardFrontFragment(); mCardFrontFragment.setOnChangeListener(new CardFrontFragment.onChangeListener() { @Override public void onChange() { flipCard(); } }); mCardBackFragment = new CardBackFragment(); mCardBackFragment.setOnChangeListener(new CardFrontFragment.onChangeListener() { @Override public void onChange() { flipCard(); } }); if (savedInstanceState == null) { getFragmentManager() .beginTransaction() .add(R.id.container, mCardFrontFragment) .commit(); } } private boolean mShowingBack; private void flipCard() { if (mShowingBack) { getFragmentManager().popBackStack(); mShowingBack = false; return; } // Flip to the back. mShowingBack = true; // Create and commit a new fragment transaction that adds the fragment for the back of // the card, uses custom animations, and is part of the fragment manager's back stack. getFragmentManager() .beginTransaction() // Replace the default fragment animations with animator resources representing // rotations when switching to the back of the card, as well as animator // resources representing rotations when flipping back to the front (e.g. when // the system Back button is pressed). .setCustomAnimations( R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out) // Replace any fragments currently in the container view with a fragment // representing the next page (indicated by the just-incremented currentPage // variable). .replace(R.id.container, mCardBackFragment) // Add this transaction to the back stack, allowing users to press Back // to get to the front of the card. .addToBackStack(null) // Commit the transaction. .commit(); } }