Difference between onCreate() and onStart()? [duplicate]
Asked Answered
R

2

166

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?

Rainout answered 25/7, 2011 at 5:10 Comment(0)
V
373

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.

Velutinous answered 25/7, 2011 at 5:21 Comment(0)
H
31

onCreate() method gets called when activity gets created, and its called only once in whole Activity life cycle. where as onStart() is called when activity is stopped... I mean it has gone to background and its onStop() method is called by the os. onStart() may be called multiple times in Activity life cycle.More details here

Hellenic answered 25/7, 2011 at 5:15 Comment(2)
onCreate() method gets called when activity created and onStart() method called when activity comes from onPause() or onResume() .Microdot
onCreate() , will be called in two scenarios , one when activity is first created, and second when activity goes through configuration changes. It is mandatory to implement onCreate() in our activity. onStart() can be called multiple times. It is not mandatory to implement onStart().Goodspeed

© 2022 - 2024 — McMap. All rights reserved.