8

In my activity I have added the fragment by using the following code.

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.right_to_left_in, R.anim.right_to_left_exit,R.anim.left_to_right_in,R.anim.left_to_right_exit);
DetailsFragment newFragment = DetailsFragment.newInstance();
ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");
ft.commit();

Fragment is entering,exiting, popping with the animations properly. But when I orient the device, Fragment Manager is trying to add the fragment with the same animations. It seems very odd. I don't want the animation when user orients the device.

I don't want to add onConfigChanges='orientation' in manifest since I want to change the fragment's layout design on orientation.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ponsuyambu
  • 7,876
  • 5
  • 27
  • 41

2 Answers2

3

The only way I could avoid this was to not retain the fragment instance. In your DetailsFragment's onCreate method use setRetainInstance(false);

AdamVe
  • 3,236
  • 2
  • 18
  • 13
  • Kudos! Its working! I am checking on other side effects(Is anything there!?) while setting setRetainInstance(false); – Ponsuyambu Jul 15 '14 at 02:51
  • 1
    The problem is that the fragment will not be retained - read more here: http://stackoverflow.com/a/11318942/667202 – AdamVe Jul 15 '14 at 05:26
  • Look also here - http://adblogcat.com/fragment-transition-animations-while-hiding-the-previous-fragment/ – AdamVe Jul 15 '14 at 06:04
0

Android re-attaches existing fragment to an activity automatically in case of orientation change. So you don't have to do it manually. You may check savedInstanceState variable in onCreate method of the activity for null and replace a fragment with animation only in case if it's null:

if (savedInstanceState == null) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.right_to_left_in, R.anim.right_to_left_exit,R.anim.left_to_right_in,R.anim.left_to_right_exit);
    DetailsFragment newFragment = DetailsFragment.newInstance();
    ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");
    ft.commit();
}
makovkastar
  • 5,000
  • 2
  • 30
  • 50
  • Thanks for your answer. My activity already has this logic. I never added the fragment again.When fragment manager tries to re-attach the fragment on orientation, it applies the animations which already stored in the transaction. (I.e which I set when fragment was added) – Ponsuyambu Jun 19 '14 at 15:09