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.
fragment1.xml:

fragment2.xml:

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 Fragment1.java 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.
@Override
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()

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

);

return view;

// in onAttach we should instantiate the listener
@Override
public void onAttach(Activity exercise)
super.onAttach(exercise);
attempt
mOnFragment1ClickedListener = (OnFragment1ClickedListener) exercise;
catch (ClassCastException e)
e.printStackTrace();

// 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 Fragment2.java 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

@Override
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()

@Override
public void onClick(View v)
Toast.makeText(getActivity(), “THIS IS FRAGMENT2 BEING CLICKED!!! PRESS BACK TO GO TO FRAGMENT1”, Toast.LENGTH_SHORT).show();

);
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.

fragment_animation_fade_in.xml:

fragment_animation_fade_out.xml:

fragment_animation_no_fade_in.xml

So this was the straightforward part. Now we need to put every thing together. We do that by creating an activity, Major.java, 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;

@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.format.important);

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();
mFragmentStack.add(tag);
transaction.add(R.id.fragment_swap, fragment,tag);
transaction.addToBackStack(tag);
transaction.commit();

// Discover how this class implements OnFragment1ClickkedListener? Properly this causes us to
// implement the abstract technique onFragment1Clicked() from Fragment1.java.
@Override
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();
newFragment.setArguments(args);

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.disguise(currentFragment);

transaction.add(R.id.fragment_swap, newFragment,newFragment.toString());
// This is essential, we must addToBackStack so we will pull it out later.
transaction.addToBackStack(tag);
// 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.
mFragmentStack.add(tag);

transaction.commit();

@Override
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();

else
// Remove the framg
removeFragment();
super.onBackPressed();

personal void removeFragment()
// take away the current fragment from the stack.
mFragmentStack.pop();
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.
transaction.present(fragment);
transaction.commit();

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?