Android Blog fragment Fragments

Fragment transition animations while hiding the previous fragment and using backstack

Print Friendly

This article will explain the way to create fragments inside an Activity, methods to dynamically add a fragment to an Exercise, the right way to create transition animations between fragments, find out how to disguise the previous fragment and finally the way to use the fragment backstack.

You possibly can obtain the code right here

So first lets get started. We need to create three format information, so create principal.xml, fragment1.xml, fragment2.xml.
most important.xml:

We shall be using the FrameLayout to do the fragment swaping.


With fragment2, I need to present that you do not want a Format View in an xml, you’ll be able to inflate any View you need. In this case, a TextView.

Now we have to create a category which extends Fragment. I have put feedback in the code so its comprehensible, the code:

public class Fragment1 extends Fragment

// my custom listener
personal OnFragment1ClickedListener mOnFragment1ClickedListener;

// Should all the time name onCreateView and inflate your format.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)

super.onCreateView(inflater, container, savedInstanceState);

View view = inflater.inflate(R.format.fragment1, container, false);

// I would like the entire view to be clickable, however I might have referred to as the textView or ImageView only.
view.setOnClickListener(new View.OnClickListener()

public void onClick(View v)
// name the listener’s summary technique, the implementation of the technique happens in the Activity.


return view;

// in onAttach we should instantiate the listener
public void onAttach(Activity exercise)
mOnFragment1ClickedListener = (OnFragment1ClickedListener) exercise;
catch (ClassCastException e)

// That is my own customized listener so that the Activity and fragment can talk.
// In case you are not conversant in this, google “observer design pattern”.
public interface OnFragment1ClickedListener
public void onFragment1Clicked();

This fragment has the click listener to name my custom listener’s abstract technique onFragment1Clicked().

We now create another class by which we do the exact same thing. Nevertheless, this fragment won’t name another fragment, so we should not have the interface and summary technique. As an alternative we just call a toast. Code:

public class Fragment2 extends Fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)

tremendous.onCreateView(inflater, container, savedInstanceState);

View view = inflater.inflate(R.format.fragment2, container, false);
view.setOnClickListener(new View.OnClickListener()

public void onClick(View v)

return view;

Now, we’d like three animations. I created one animation that fades away and one other one that seems as the other fades and lastly one which just seems so when users press the again button, they don’t have to wait for the fading animation; it doesn’t make sense. We have to create a folder inside the res folder “anim” and inside create three xml information: fragment_animation_fade_in.xml, fragment_animation_fade_out.xml, fragment_animation_no_fade_in.xml.
Discover that with fragments we now have to make use of objectAnimator.




So this was the straightforward part. Now we need to put every thing together. We do that by creating an activity,, which can do all the work. Here is the code with comments and then I’ll clarify additional:

public class Primary extends Exercise implements OnFragment1ClickedListener
// This is needed as a way to manage the fragments.
public Stack mFragmentStack;

public void onCreate(Bundle savedInstanceState)

mFragmentStack = new Stack();

// Dynamically add a fragment to an Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = new Fragment1();
String tag = fragment.toString();
transaction.add(, fragment,tag);

// Discover how this class implements OnFragment1ClickkedListener? Properly this causes us to
// implement the abstract technique onFragment1Clicked() from
public void onFragment1Clicked()
Fragment newFragment = new Fragment2();
Bundle args = new Bundle();
// by doing newFragment.toString(), I’m taking a singular recognized of that object.
String tag = newFragment.toString();

FragmentTransaction transaction = getFragmentManager().beginTransaction();
// setting the animations right here. the fade_in animation for fragment1 and fade_out for fragment2
transaction.setCustomAnimations(R.anim.fragment_animation_fade_in, R.anim.fragment_animation_fade_out);
// I discover what the present fragment from the stack is, take it and disguise it
// using transaction.cover(currentFragment);
Fragment currentFragment = getFragmentManager().findFragmentByTag(mFragmentStack.peek());

transaction.add(, newFragment,newFragment.toString());
// This is essential, we must addToBackStack so we will pull it out later.
// As an alternative of using substitute we use add. Why? If we use exchange, then the previous
// fragment will all the time need to be re-created. What in case you don’t need to do this. In
// my case, I didn’t want it to be re-created all the time because I had a place
// set and by re-creating it, I might have misplaced the place or had to make use of static flag.


public void onBackPressed()
// from the stack we will get the latest fragment
Fragment fragment = getFragmentManager().findFragmentByTag(mFragmentStack.peek());
// If its an occasion of Fragment1 I don’t need to end my activity, so I launch a Toast as an alternative.
if (fragment instanceof Fragment1)
Toast.makeText(getApplicationContext(), “YOU ARE AT THE TOP FRAGMENT”, Toast.LENGTH_SHORT).present();

// Remove the framg

personal void removeFragment()
// take away the current fragment from the stack.
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// get fragment that’s to be proven (in our case fragment1).
Fragment fragment = getFragmentManager().findFragmentByTag(mFragmentStack.peek());
// This time I set an animation with no fade in, so the consumer doesn’t watch for the animation in back press
transaction.setCustomAnimations(R.anim.fragment_animation_no_fade_in, R.anim.fragment_animation_fade_out);
// We should use the show() technique.

So principally I hold a stack of fragments that I’m using so I can use as many fragments as I would like. I am only using two proper now, but it will work with three or more as properly so long as the code in onFragment1Clicked() is used (slightly modified for different fragments in fact). Also I’m using the FragmentTransaction technique disguise() to hide my previous fragments and as an alternative of exchange() I am using add(). That is all, comfortable fragmenting?