A real approach to preventing the re-creation of a fragment after turning the screen (example guide for official fragments)

I find a real approach to prevent the re-creation of the fragment after turning the screen

if (container == null) { return null; } not by actually avoiding re-creating the fragment. (shown below)


Where is the official snippet developers guide?

The official guide we are talking about is at http://developer.android.com/guide/components/fragments.html . A partial sample code is at the bottom of the manual. As far as I know, a complete sample code is available in the Samples for SDK under Android 3.0 (API 11). In addition, I performed a minimal modification of the sample code to run it in API 10 and added some debugging messages, which are included at the end of this question.

Where R.id.a_item?

You can find the following code in the Example Developer's Guide :

if (index == 0) {
    ft.replace(R.id.details, details);
} else {
    ft.replace(R.id.a_item, details);
}

, R.id.a_item. API 11, , . .


?

. , , "" .

, DetailsFragment. (1) , (2) , (3) , (4) , (5) , , (6) . :

(1)

TitlesFragment.onCreate() Bundle = null

TitlesFragment. DetailsFragment .

(2)

TitlesFragment.onCreate() Bundle = Bundle [{Choice = -1, android: view_state=android.util.SparseArray@4051d3a8, curChoice = 0}]
DetailsFragment.onAttach() Activity=com.example.android.apis.app.FragmentLayout@4051d640
DetailsFragment.onCreate() Bundle = null
DetailsFragment.onCreateView() Activity=android.widget.FrameLayout@4050df68
DetailsFragment.onActivityCreated() Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()

-, TitlesFragment ( savedInstanceState Bundle). DetailsFragment ( TitlesFragment.onActivityCreated(), showDetails(), FragmentTransaction).

(3)

DetailsFragment.onPause()
DetailsFragment.onStop()
DetailsFragment.onDestroyView()
DetailsFragment.onDestroy()
DetailsFragment.onDetach()
DetailsFragment.onAttach() Activity=com.example.android.apis.app.FragmentLayout@40527f70
DetailsFragment.onCreate() Bundle = null
TitlesFragment.onCreate() Bundle = Bundle [{Choice = 0, android: view_state=android.util.SparseArray@405144b0, curChoice = 0}]
DetailsFragment.onCreateView() Activity = null
DetailsFragment.onActivityCreated() Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()

, .

, DetailsFragment layout-land/fragment_layout.xml <FrameLayout> ViewGroup . (R.id.details). , ViewGroup, DetailsFragment, FragmentLayout Activity, FragmentLayout onSaveInstanceState(). DetailsFragment. .

(, ), DetailsFragment if (container == null) { return null; } onCreateView(), DetailsFragment . , , DetailsFragment , , .

(4)

DetailsFragment.onPause()
DetailsFragment.onStop()
DetailsFragment.onDestroyView()
DetailsFragment.onDestroy()
DetailsFragment.onDetach()
DetailsFragment.onAttach() Activity=com.example.android.apis.app.FragmentLayout@4052c7d8
DetailsFragment.onCreate() Bundle = null
TitlesFragment.onCreate() Bundle = Bundle [{Choice = 0, android: view_state=android.util.SparseArray@40521b80, curChoice = 0}]
DetailsFragment.onCreateView() Activity=android.widget.FrameLayout@40525270
DetailsFragment.onActivityCreated() Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()

, 5 DetailsFragment , .

, if (container == null) { return null; } - a , DetailsFragment. ( , , , Android . : .)

, 6- , DetailsFragment, TitlesFragment, (2). , DetailsFragment onAttach() onCreate() TitlesFragment onCreate().

null DetailsFragment onCreate() , .

, DetailsFragment , . , savedInstanceState.

(5)

DetailsFragment.onPause()
DetailsFragment.onStop()
DetailsFragment.onDestroyView()
DetailsFragment.onDestroy()
DetailsFragment.onDetach()
DetailsFragment.onAttach() Activity=com.example.android.apis.app.FragmentLayout@4052d7d8
DetailsFragment.onCreate() Bundle = null
TitlesFragment.onCreate() Bundle = Bundle [{Choice = 0, android: view_state=android.util.SparseArray@40534e30, curChoice = 0}]
DetailsFragment.onCreateView() Activity = null
DetailsFragment.onActivityCreated() Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()

, (3), Activity ID (40527f70 vs 4052d7d8) view_state Bundle (405144b0 vs 40534e30). . FragmentLayout Activity, State State Bundle.

(6) ( BACK)

I/System.out(29845): DetailsFragment.onPause() I/System.out(29845): DetailsFragment.onStop() I/System.out(29845): DetailsFragment.onDestroyView() I/System.out(29845): DetailsFragment.onDestroy() I/System.out(29845): DetailsFragment.onDetach()

, DetailsFragment FragmentLayout onDestroy(). FragmentTransaction remove() onSaveInstanceState(). , onSaveInstanceState().

DetailsFragment FragmentLayout onSaveInstanceState(). -, DetailsFragment , . , onCreate(Bundle) onRestoreInstanceState(Bundle) . , ( Bundle).


FragmentLayout.java

package com.example.android.apis.app;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class FragmentLayout extends FragmentActivity {

    private final static class Shakespeare {
        public static final String[] TITLES = { "Love", "Hate", "One", "Day" };
        public static final String[] DIALOGUE = {
            "Love Love Love Love Love",
            "Hate Hate Hate Hate Hate",
            "One One One One One",
            "Day Day Day Day Day" };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_layout);
    }

    public static class DetailsActivity extends FragmentActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if (getResources().getConfiguration().orientation
                    == Configuration.ORIENTATION_LANDSCAPE) {
                // If the screen is now in landscape mode, we can show the
                // dialog in-line with the list so we don't need this activity.
                finish();
                return;
            }

            if (savedInstanceState == null) {
                // During initial setup, plug in the details fragment.
                DetailsFragment details = new DetailsFragment();
                details.setArguments(getIntent().getExtras());
                getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
            }
        }
    }

    public static class TitlesFragment extends ListFragment {
        boolean mDualPane;
        int mCurCheckPosition = 0;
        int mShownCheckPosition = -1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            System.out.println(getClass().getSimpleName() + ".onCreate() Bundle=" + 
                    (savedInstanceState == null ? null : savedInstanceState));
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            // Populate list with our static array of titles.
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, Shakespeare.TITLES));
            // API 11:android.R.layout.simple_list_item_activated_1

            // Check to see if we have a frame in which to embed the details
            // fragment directly in the containing UI.
            View detailsFrame = getActivity().findViewById(R.id.details);
            mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

            if (savedInstanceState != null) {
                // Restore last state for checked position.
                mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
                mShownCheckPosition = savedInstanceState.getInt("shownChoice", -1);
            }

            if (mDualPane) {
                // In dual-pane mode, the list view highlights the selected item.
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                // Make sure our UI is in the correct state.
                showDetails(mCurCheckPosition);
            }
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt("curChoice", mCurCheckPosition);
            outState.putInt("shownChoice", mShownCheckPosition);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            showDetails(position);
        }

        /**
         * Helper function to show the details of a selected item, either by
         * displaying a fragment in-place in the current UI, or starting a
         * whole new activity in which it is displayed.
         */
        void showDetails(int index) {
            mCurCheckPosition = index;

            if (mDualPane) {
                // We can display everything in-place with fragments, so update
                // the list to highlight the selected item and show the data.
                getListView().setItemChecked(index, true);

                if (mShownCheckPosition != mCurCheckPosition) {
                    // If we are not currently showing a fragment for the new
                    // position, we need to create and install a new one.
                    DetailsFragment df = DetailsFragment.newInstance(index);

                    // Execute a transaction, replacing any existing fragment
                    // with this one inside the frame.
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.details, df);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.commit();
                    mShownCheckPosition = index;
                }

            } else {
                // Otherwise we need to launch a new activity to display
                // the dialog fragment with selected text.
                Intent intent = new Intent();
                intent.setClass(getActivity(), DetailsActivity.class);
                intent.putExtra("index", index);
                startActivity(intent);
            }
        }
    }

    public static class DetailsFragment extends Fragment {
        /**
         * Create a new instance of DetailsFragment, initialized to
         * show the text at 'index'.
         */
        public static DetailsFragment newInstance(int index) {
            DetailsFragment f = new DetailsFragment();

            // Supply index input as an argument.
            Bundle args = new Bundle();
            args.putInt("index", index);
            f.setArguments(args);

            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            System.out.println(getClass().getSimpleName() + ".onCreate() Bundle=" + 
                    (savedInstanceState == null ? null : savedInstanceState));
        }
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            System.out.println(getClass().getSimpleName() + ".onAttach() Activity=" + 
                    (activity == null ? null : activity));
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            System.out.println(getClass().getSimpleName() + ".onActivityCreated() Bundle=" + 
                    (savedInstanceState == null ? null : savedInstanceState));
        }
        @Override
        public void onStart() { super.onStart(); System.out.println(getClass().getSimpleName() + ".onStart()"); }
        @Override
        public void onResume() { super.onResume(); System.out.println(getClass().getSimpleName() + ".onResume()"); }
        @Override
        public void onPause() { super.onPause(); System.out.println(getClass().getSimpleName() + ".onPause()"); }
        @Override
        public void onStop() { super.onStop(); System.out.println(getClass().getSimpleName() + ".onStop()"); }
        @Override
        public void onDestroyView() { super.onDestroyView(); System.out.println(getClass().getSimpleName() + ".onDestroyView()"); }
        @Override
        public void onDestroy() { super.onDestroy(); System.out.println(getClass().getSimpleName() + ".onDestroy()"); }
        @Override
        public void onDetach() { super.onDetach(); System.out.println(getClass().getSimpleName() + ".onDetach()"); }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            System.out.println(getClass().getSimpleName() + ".onCreateView() Activity=" + 
                    (container == null ? null : container));

            if (container == null) {
                // We have different layouts, and in one of them this
                // fragment containing frame doesn't exist.  The fragment
                // may still be created from its saved state, but there is
                // no reason to try to create its view hierarchy because it
                // won't be displayed.  Note this is not needed -- we could
                // just run the code below, where we would create and return
                // the view hierarchy; it would just never be used.
                return null;
            }

            ScrollView scroller = new ScrollView(getActivity());
            TextView text = new TextView(getActivity());
            int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    4, getActivity().getResources().getDisplayMetrics());
            text.setPadding(padding, padding, padding, padding);
            scroller.addView(text);
            text.setText(Shakespeare.DIALOGUE[getArguments().getInt("index", 0)]);
            return scroller;
        }
    }

}

/fragment _layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

-/fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:baselineAligned="false"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />
    <!-- API 11:android:background="?android:attr/detailsElementBackground" -->

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.apis.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android.apis.app.FragmentLayout"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.android.apis.app.FragmentLayout$DetailsActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
0
2

@RogerGarzonNieto , . . , .

, , , , Activity.

onSaveInstanceState():

@Override
protected void onSaveInstanceState(Bundle outState) {
    if (isPortrait2Landscape()) {
        remove_fragments();
    }
    super.onSaveInstanceState(outState);
}

private boolean isPortrait2Landscape() {
    return isDevicePortrait() && (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}

isDevicePortrait() :

private boolean isDevicePortrait() {
    return (findViewById(R.id.A_View_Only_In_Portrait) != null);
}

* , getResources().getConfiguration().orientation, , "". , Resources RIGHT AFTER, - onSaveInstanceState() !!

findViewById() ( - , ), private int current_orientation; current_orientation = getResources().getConfiguration().orientation; onCreate(). . , .

* , remove_fragments() super.onSaveInstanceState().

( Activity. super.onSaveInstanceState(), Bundle, Activity. ###)

### . ? - , . - , . !

-3

.

, .

public void activate(FragmentTransaction ft, Fragment f, String tag, int resId) {
    boolean changed =   resId != f.getId();

    if (changed && (f.isAdded() || f.isDetached())) {
        ft.remove(f);
        ft.add(resId, f, tag);
        return;
    }

    // Currently in a detached mode
    if (f.isDetached()) {
        ft.attach(f);
        return;
    }

    // Not in fragment manager add
    if (!f.isAdded() && ! f.isDetached()) {
        ft.add(resId, f, tag);
        return;
    }
}

memoized .

private enum FragmentOp {
    ADD
    ,DETACH
    ,REMOVE
    ;
}

private void ensureFragmentState(FragmentOp op) {
    if (null == iChild) {
        iChild = (FragmentChild) iFM.findFragmentById(R.id.fragment_phonenumber_details);
    }

    FragmentTransaction ft = iFM.beginTransaction();
    switch(op) {
    case ADD:
        if (null == iChild) {
            iChild = new FragmentChild();
        }
        activate(ft, iChild, null, R.id.fragment_phonenumber_details);
        break;
    case DETACH:
        if (null != iChild) {
            iChild.deactivate(ft);
        }
        break;
    case REMOVE:
        if (null != iChild) {
            iChild.remove(ft);
        }
        break;
    }
    // Only if something shows up did we do anything!
    if (null != iChild) {
        ft.commit();
    }
}

:

@Override public void onResume() {
    super.onResume();
    if (iDualPane) {
        ensureFragmentState(FragmentOp.ADD);
    }
}

@Override public void onPause() {
    super.onPause();

    recordState();   // Grab what I need from the child fragment

    ensureFragmentState(FragmentOp.DETACH);
}
0

All Articles