androidandroid-activityoncreateonstart

Difference between onCreate() and onStart()?


Possible Duplicate:
Android Activity Life Cycle - difference between onPause() and OnStop()

I was wondering - what is the difference between onCreate() and onStart() methods?

I think that onStart() is a redundant method. onCreate() will ALWAYS be called (At least in my last two projects).

Can any one explain the difference?


Solution

  • Take a look at the life cycle of Activity.

    The following image and documentation excerpt are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

    enter image description here

    Where

    ***onCreate()***
    

    Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

    ***onStart()***
    

    Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

    And you can write your simple class to take a look when these methods call. The following code is adapted and simplified from the example and demo application found in Lifecycle Methods in Details, which is a very good article to understand the life cycle.

    public class TestActivity extends Activity {
        /** Called when the activity is first created. */
    
        private final static String TAG = "TestActivity";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Log.i(TAG, "On Create .....");
        }
        /* (non-Javadoc)
        * @see android.app.Activity#onDestroy()
        */
        @Override
        protected void onDestroy() { 
            super.onDestroy();
            Log.i(TAG, "On Destroy .....");
        }
        /* (non-Javadoc)
        * @see android.app.Activity#onPause()
        */
        @Override
        protected void onPause() { 
            super.onPause();
            Log.i(TAG, "On Pause .....");
        }
    
        /* (non-Javadoc)
        * @see android.app.Activity#onRestart()
        */
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.i(TAG, "On Restart .....");
        }
    
        /* (non-Javadoc)
        * @see android.app.Activity#onResume()
        */
        @Override
        protected void onResume() {
            super.onResume();
            Log.i(TAG, "On Resume .....");
        }
    
        /* (non-Javadoc)
        * @see android.app.Activity#onStart()
        */
        @Override
        protected void onStart() {
            super.onStart();
            Log.i(TAG, "On Start .....");
        }
        /* (non-Javadoc)
        * @see android.app.Activity#onStop()
        */
        @Override
        protected void onStop() {
            super.onStop();
            Log.i(TAG, "On Stop .....");
        }
    }
    

    Link to android documentation Activity for details.