Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customAnimation when calling popBackStack on a FragmentManager

In my activity, with the touch of a button, I replace the current fragment with a new fragment using a custom animation, like in this example.

@Override public boolean onOptionsItemSelected(MenuItem item) {     // Handle presses on the action bar items     switch (item.getItemId()) {         case R.id.action_anomalie:             Fragment contentFragment = getFragmentManager().findFragmentById(R.id.content);              if(contentFragment instanceof AnomalieListFragment)             {                 getFragmentManager().popBackStack();                 return true;             }             else             {                 FragmentTransaction ft = getFragmentManager().beginTransaction();                 ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);                 anomalieFragment = new AnomalieListFragment();                 ft.replace(R.id.content, anomalieFragment);                 ft.addToBackStack(null);                 ft.commit();             }      ... 

However, popping back the stack doesn't show any animation. Is there a way to specify a custom animation like we do in a FragmentTransaction with the setCustomAnimations method?

like image 777
Goldorak84 Avatar asked Aug 28 '14 13:08

Goldorak84


People also ask

What is addToBackStack in android?

public abstract FragmentTransaction addToBackStack (String name) Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

What is FragmentManager in Android?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.

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.


1 Answers

After further reading of the documentation, I found that using this signature of setCustomAnimation allowed the animation to be played when pressing the back button or calling getFragmentManager().popBackStack();

I modified my code like this

... FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out); anomalieFragment = new AnomalieListFragment(); ft.replace(R.id.content, anomalieFragment); ft.addToBackStack(null); ft.commit(); ... 
like image 106
Goldorak84 Avatar answered Sep 23 '22 21:09

Goldorak84