[android-developers] Re: Hidden fragments (and animations) after a rotation
3/ In your code you re-add the Fragment to the container even if it was > found. It doesn't do harm, but this is unnecessary because the framework > already does it for you. The FragmentTransaction should be inside the if. > You are right, the fragment needs to be added to the container only the first time. Fixed in my code. Thanks. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Hidden fragments (and animations) after a rotation
Thanks for answering. However, 1/ I test savedInstanceState because when it is null it means the Activity was created for the first time (in which case I instantiate and add the fragment). After a rotation it is not null - I agree it is not clear and I should look for the Fragment with a tag instead, like you do, or at least put a comment there ;) 2/ You should not create and add the Fragment every time the activity is created *whether you use setRetainInstance(true) or not*. I really think it is not related, please correct me if I'm wrong. 3/ In your code you re-add the Fragment to the container even if it was found. It doesn't do harm, but this is unnecessary because the framework already does it for you. The FragmentTransaction should be inside the if. In any case this was not the cause of my problem, and I still reproduce it with the updated code below. In other words it seems that: 1/ The hidden / visible state of a Fragment is not kept when rotating the screen (unless setRetainInstance(true) is called) 2/ The custom animations are not saved in the back stack when rotating the screen Can anyone confirm this? Thanks! public class MainActivity extends Activity { private static final String FRAGMENT_TEST = "FRAGMENT_TEST"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TestFragment testFragment = (TestFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TEST); if (testFragment == null) { testFragment = new TestFragment(); // testFragment.setRetainInstance(true); final FragmentTransaction ftAdd = getFragmentManager().beginTransaction(); ftAdd.add(R.id.container, testFragment, FRAGMENT_TEST); ftAdd.addToBackStack(null); ftAdd.commit(); final FragmentTransaction ftHide = getFragmentManager().beginTransaction(); ftHide.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out); ftHide.hide(testFragment); ftHide.addToBackStack(null); ftHide.commit(); } } public static class TestFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final TextView res = new TextView(getActivity()); res.setText("Fragment " + toString()); return res; } } } -- BoD On Friday, April 20, 2012 4:13:26 PM UTC+2, Nadeem Hasan wrote: > > I am not exactly sure if this is the cause of your problem but your > fragment creation code is flawed. When you use *setRetainInstance(true)* you > don't need to create the fragment every time the activity is created. Also, > I don't know why you check for saveInstance to be null. When the phone is > rotated you should just retrieve the existing fragment instance from the > FragmentManager. I use something like below in mu base Activity: > > protected Fragment addFragment( Class clss ) { > String tag = clss.getSimpleName(); > FragmentManager fm = getSupportFragmentManager(); > Fragment f = fm.findFragmentByTag( tag ); > if ( f == null ) { > f = Fragment.instantiate( this, clss.getName() ); > } > FragmentTransaction ft = fm.beginTransaction(); > ft.add( R.id.fragment_container, f, tag ); > ft.commit(); > return f; > } > > > And then in onCreate() of my Activity I simply call: > > MyFragment f = addFragment( MyFragment.class ); > > This means that a Fragment instance is created the first time the Activity > is created but on any subsequent rotations, the existing Fragment instance > is retrieved from the FragmentManager is attached to the activity. > > Your code creates a new Fragment instance every time the device is > rotated, which may lead to other issues like Fragment instances with no > attached Activity. > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Hidden fragments (and animations) after a rotation
I am not exactly sure if this is the cause of your problem but your fragment creation code is flawed. When you use *setRetainInstance(true)* you don't need to create the fragment every time the activity is created. Also, I don't know why you check for saveInstance to be null. When the phone is rotated you should just retrieve the existing fragment instance from the FragmentManager. I use something like below in mu base Activity: protected Fragment addFragment( Class clss ) { String tag = clss.getSimpleName(); FragmentManager fm = getSupportFragmentManager(); Fragment f = fm.findFragmentByTag( tag ); if ( f == null ) { f = Fragment.instantiate( this, clss.getName() ); } FragmentTransaction ft = fm.beginTransaction(); ft.add( R.id.fragment_container, f, tag ); ft.commit(); return f; } And then in onCreate() of my Activity I simply call: MyFragment f = addFragment( MyFragment.class ); This means that a Fragment instance is created the first time the Activity is created but on any subsequent rotations, the existing Fragment instance is retrieved from the FragmentManager is attached to the activity. Your code creates a new Fragment instance every time the device is rotated, which may lead to other issues like Fragment instances with no attached Activity. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Hidden fragments (and animations) after a rotation
This wouldn't be the first time when the support library is somewhat inconsistent with native platform code. Have you tried debugging into the library code? The sources are available under android-sdk\extras\android\compatibility\v4\src. -- K 20.04.2012 11:43, BoD ???: Thanks for answering. However I already knew that, and it's not really the point of my question :) -- BoD On 04/20/2012 01:13 AM, lbendlin wrote: Rotating the screen restarts the activity unless you explicitly tell the OS not to do that. On Thursday, April 19, 2012 7:38:52 AM UTC-4, BoD wrote: Hi! I have an activity with a fragment that I hide in a transaction with a custom animation. This transaction is added to the back stack. If I don't rotate the screen this is working as expected (pressing 'back' un-hides the fragment with the animation) 1/ If I rotate the screen, the fragment is no longer hidden. I discovered that if I call setRetainInstance(true) on the fragment however, this problem disappears. But is this expected and/or documented somewhere? 2/ But even if I do that, pressing 'back' shout un-hide the fragment, with the custom animation. However: - Not using the support package, on a 3.2 Xoom, the fragment stays hidden. - Using the support package, the fragment does become visible again, but with no animation. Is this a (known) bug or did I miss something? Thanks a lot for your help! Here is the simple example demonstrating this (replace FragmentActivity by Activity and comment/uncomment to switch from using the support package or not): public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (savedInstanceState == null) { final TestFragment testFragment = new TestFragment(); testFragment.setRetainInstance(true); final FragmentTransaction ftAdd = getSupportFragmentManager().beginTransaction(); // final FragmentTransaction ftAdd = getFragmentManager().beginTransaction(); ftAdd.add(R.id.container, testFragment); ftAdd.addToBackStack(null); ftAdd.commit(); final FragmentTransaction ftHide = getSupportFragmentManager().beginTransaction(); // final FragmentTransaction ftHide = getFragmentManager().beginTransaction(); // ftHide.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out); ftHide.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out); ftHide.hide(testFragment); ftHide.addToBackStack(null); ftHide.commit(); } } public static class TestFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final TextView res = new TextView(getActivity()); res.setText("Fragment"); return res; } } } -- BoD -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Kostya Vasilyev -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Hidden fragments (and animations) after a rotation
Thanks for answering. However I already knew that, and it's not really the point of my question :) -- BoD On 04/20/2012 01:13 AM, lbendlin wrote: Rotating the screen restarts the activity unless you explicitly tell the OS not to do that. On Thursday, April 19, 2012 7:38:52 AM UTC-4, BoD wrote: Hi! I have an activity with a fragment that I hide in a transaction with a custom animation. This transaction is added to the back stack. If I don't rotate the screen this is working as expected (pressing 'back' un-hides the fragment with the animation) 1/ If I rotate the screen, the fragment is no longer hidden. I discovered that if I call setRetainInstance(true) on the fragment however, this problem disappears. But is this expected and/or documented somewhere? 2/ But even if I do that, pressing 'back' shout un-hide the fragment, with the custom animation. However: - Not using the support package, on a 3.2 Xoom, the fragment stays hidden. - Using the support package, the fragment does become visible again, but with no animation. Is this a (known) bug or did I miss something? Thanks a lot for your help! Here is the simple example demonstrating this (replace FragmentActivity by Activity and comment/uncomment to switch from using the support package or not): public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (savedInstanceState == null) { final TestFragment testFragment = new TestFragment(); testFragment.setRetainInstance(true); final FragmentTransaction ftAdd = getSupportFragmentManager().beginTransaction(); // final FragmentTransaction ftAdd = getFragmentManager().beginTransaction(); ftAdd.add(R.id.container, testFragment); ftAdd.addToBackStack(null); ftAdd.commit(); final FragmentTransaction ftHide = getSupportFragmentManager().beginTransaction(); // final FragmentTransaction ftHide = getFragmentManager().beginTransaction(); // ftHide.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out); ftHide.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out); ftHide.hide(testFragment); ftHide.addToBackStack(null); ftHide.commit(); } } public static class TestFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final TextView res = new TextView(getActivity()); res.setText("Fragment"); return res; } } } -- BoD -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Hidden fragments (and animations) after a rotation
Rotating the screen restarts the activity unless you explicitly tell the OS not to do that. On Thursday, April 19, 2012 7:38:52 AM UTC-4, BoD wrote: > > Hi! > > I have an activity with a fragment that I hide in a transaction with a > custom animation. > This transaction is added to the back stack. > If I don't rotate the screen this is working as expected (pressing > 'back' un-hides the fragment with the animation) > > 1/ If I rotate the screen, the fragment is no longer hidden. I > discovered that if I call setRetainInstance(true) on the fragment > however, this problem disappears. But is this expected and/or > documented somewhere? > > 2/ But even if I do that, pressing 'back' shout un-hide the fragment, > with the custom animation. However: > - Not using the support package, on a 3.2 Xoom, the fragment stays > hidden. > - Using the support package, the fragment does become visible again, > but with no animation. > Is this a (known) bug or did I miss something? > > Thanks a lot for your help! > > Here is the simple example demonstrating this (replace FragmentActivity > by Activity and comment/uncomment to switch from using the support > package or not): > > public class MainActivity extends FragmentActivity { > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > if (savedInstanceState == null) { > final TestFragment testFragment = new TestFragment(); > testFragment.setRetainInstance(true); > > final FragmentTransaction ftAdd = > getSupportFragmentManager().beginTransaction(); > // final FragmentTransaction ftAdd = > getFragmentManager().beginTransaction(); > ftAdd.add(R.id.container, testFragment); > ftAdd.addToBackStack(null); > ftAdd.commit(); > > final FragmentTransaction ftHide = > getSupportFragmentManager().beginTransaction(); > // final FragmentTransaction ftHide = > getFragmentManager().beginTransaction(); > // ftHide.setCustomAnimations(android.R.animator.fade_in, > android.R.animator.fade_out, android.R.animator.fade_in, > android.R.animator.fade_out); > ftHide.setCustomAnimations(android.R.anim.fade_in, > android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out); > ftHide.hide(testFragment); > ftHide.addToBackStack(null); > ftHide.commit(); > } > } > > public static class TestFragment extends Fragment { > @Override > public View onCreateView(LayoutInflater inflater, ViewGroup > container, Bundle savedInstanceState) { > final TextView res = new TextView(getActivity()); > res.setText("Fragment"); > return res; > } > } > } > > -- > BoD > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en