0

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);
    }
}
ufulol
  • 13
  • 1
  • 3
  • What do you mean by "no `OnStart()` after `OnCreate()`"? The methods are not directly chained, so you won't find any code explicitly calling one after the other. – Ken Y-N Sep 19 '17 at 03:53
  • These are 'callbacks' from the Activity lifecycle. They don't need to be explicitly redefined unless you want to modify the super's behavior. – Shaishav Sep 19 '17 at 03:53

4 Answers4

2

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.

Hasif Seyd
  • 1,686
  • 12
  • 19
  • basically, does ActivityManager calls everything that is defined and/or implemented in a class? – ufulol Sep 19 '17 at 04:12
  • @ufulol ActivityManager will take care of calling all the LifeCycle callbacks on Activity when Required. Like when Activity's Focus goes out, it calls onPause etc. – Hasif Seyd Sep 19 '17 at 04:14
  • @ufulol so depending on our logic and when to execute those logic, we have to place the code in each lifecycle method's carefully – Hasif Seyd Sep 19 '17 at 04:15
  • then why does the autogenerated code in my example override OnCreate(), and is it possible not to override it? – ufulol Sep 19 '17 at 06:15
  • @ufulol even though framework calls the onCreate Method internally when activity is created, it won't render any UI onto the Activity, for that you would have to write the UI rendering logic when the Activity is Created, for that you should override the oncreate method and put your logic – Hasif Seyd Sep 19 '17 at 06:45
1

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() { ... }

Younghwa Park
  • 392
  • 2
  • 9
  • but if it is called by the system,doesn't it mean that this call should be somewhere in MyActivity's superclass' methods, or in OnCreate() or SetContentView()'s implementations? – ufulol Sep 19 '17 at 03:56
  • I am sure super class has it. so you can override it. – Younghwa Park Sep 19 '17 at 14:19
0

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

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

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52