Why when the new Android project is started in the Android Studio, there is no explicit call of OnStart() after OnCreate() in the autogenerated code, although all the tutorials say that OnCreate() is always followed by OnStart()?Also, I looked up in the base classes like AppCompatActivity and in the implementation of OnCreate(), there is no (explicit or implicit) callback of OnStart() either. To be clear, everything works fine, I do not have any errors or problems, but there seems to be a contradiction between what I see(no OnStart() after OnCreate() ) and what the tutorials say. Could anyone clarify this?

Official Android reference site

package mypack.helloandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MyActivity extends AppCompatActivity {

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

There are 4 answers

5
Hasif Seyd On BEST ANSWER

LifeCycle callback methods are called for an Activity by ActivityManager from the System (Framework) . So you won't see any direct call of these methods inside the Activity code.

These lifecycle methods are called when required. like onCreate will be called when the Activity instance is created newly by the framework.

But onStart will be called when the Activity is Visible to the User.

0
Sergey Emeliyanov On

This is the issue of inheritance in Java. Look at the class declaration of the Activity class you have in Android - it is extending some other class like AppActivityCompat or other base Activity class.

Hence, when your code is run - everything (all methods) inside the superclass of your activity are executed. There are many of them, including all of the lifecycle methods such as onCreate(), onStart(), onResume() etc.

If you need to do some specific actions inside a method you override it inside your subclass (i.e. MainActivity), and the code inside your overriden method will run instead of the method inside superclass.

For more information read the official documentation:

https://developer.android.com/guide/components/activities/activity-lifecycle.html

2
Younghwa Park On

onStart() is called by system. You don't have to call it.

If you want some custom behavior, You can override onStart()

@Override protected void onStart() { ... }

4
Code-Apprentice On

onStart() is less commonly used than onCreate(). If you have a reason to implement onStart() you can add it yourself. I believe that the default implementation of onStart() is in Activity.

In Android we do not write a main() method as we do in "typical" Java apps. Instead, we write our code in the lifecycle call backs. These are the entry points into our apps. The Android system calls these call backs according to the contract described in the documentation.