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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to