0

I use this code from here to create a Flip Card Animation for older Android devices. Now I want to flip to a Fragment which is not in the current Activity. I tried with this code:

MyFragment f = new MyFragment();

getActivity().getSupportFragmentManager().beginTransaction().add(R.id.item_detail_container, f).commit();
getActivity().getSupportFragmentManager().executePendingTransactions();

f.getView().setVisibility(View.GONE);
View root = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);

FlipAnimation flipAnimation = new FlipAnimation(this.getView(), f.getView());
root.startAnimation(flipAnimation);

But then it comes to a NullPointerException on toView.setVisibility(View.VISIBLE); in the FlipAnimation class. How can I flip to a Fragment which is not in the current Activity?

EDIT: I solve the question with the answer 1 and the comments from it. The Code here is updated and works fine now.

Community
  • 1
  • 1
Cilenco
  • 6,951
  • 17
  • 72
  • 152

1 Answers1

2

commit() on a FragmentTransaction is not immediate. It is asynchronous. Hence, by the time commit() returns, your fragment has not been called with onCreateView() yet, and therefore getView() returns null.

I would think that the right answer would be for you to be applying your animations right in the FragmentTransaction via setCustomAnimations().

Beyond that, you could try to post() your work to do the FlipAnimation, so it occurs after the transaction has been completed.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think the olution with with `setCustomAnimations()` does not work on older devices because I have to give some xml resources to the method and in the xml are ObjectAnimator which only work on Android > 3.0 The solution with post should work I think. But how should I give the FlipAnimation Object to the Method and on which Fragment do I have to call it? Can you please give me a sample code? – Cilenco May 19 '13 at 20:45
  • @Cilenco: I have never tried performing an animation the way you are, so I do not have any sample code. I am merely pointing out that you cannot do your animation where you are, because the `View` you wish to animate does not exist. You might consider only doing the animations on devices that support `setCustomAniamtions()`. – CommonsWare May 19 '13 at 20:51
  • I got it :) `commit() on a FragmentTransaction is not immediate. It is asynchronous.` That's the important thing. I have to call `executePendingTransactions()` then the onCreateView is called and all works fine. I updated my first post. Thank you for your help. – Cilenco May 19 '13 at 21:12
  • If I want to navigate back and do it with the code above I create again a new Object. Do I have to delete them anyway in the activity? I'm scared that I fill the memory until it stops if I change the fragment to often. Can that happen? Do I have to do anything after the change? In my eyes it is a kind of recursion. – Cilenco May 20 '13 at 16:10
  • @Cilenco: Again, I have never tried doing what you are doing, and so I do not know all of its ramifications. – CommonsWare May 20 '13 at 19:12