Android onCreate Method

When you first create a new project with an empty activity, you’ll get a Java file that looks something like this:

public class MainActivity extends AppCompatActivity {

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

}

An Activity is (sort of) a screen in your app. However, the activity is actually an invisible object in memory, and your app needs to do some work to translate that into something that will appear on your screen.

The Java code above is a class that extends (inherits from) the Activity class.

Every activity needs to have an onCreate method. This is the method that runs when the screen first opens.

You don’t want to delete these lines of code from onCreate():

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Those cause your layout to display on the screen.
(Remember when you created a layout?)

onCreate() method is like a Screen Initialize block from MIT App Inventor:

If you want something to happen as soon as the user opens this screen, you can add it to the end of the onCreate() method:

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Add code here to make something happen
        //as soon as the screen opens
    }

You will often create View objects in that part of your code.