Code example for Fragment

Methods: onCreate, setRetainInstance

0
         * Fragment initialization.  We way we want to be retained and 
         * start our thread. 
         */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
 
            // Tell the framework to try to keep this fragment around 
            // during a configuration change. 
            setRetainInstance(true);
 
            // Start up the worker thread. 
            mThread.start();
        } 
 
        /** 
         * This is called when the Fragment's Activity is ready to go, after 
         * its content view has been installed; it is called both after 
         * the initial fragment creation and after the fragment is re-attached 
         * to a new activity. 
         */