Now let's look at our MainActivity.java
. This is already connected to our XML file so we don't need to do that manually. Right now our code looks like this:
package com.tristanwiley.cse410;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The first few lines are pretty ordinary, our package name and import statements. We have our MainActivity class that extends AppCompatActivity which is essentially an Android activity that's compatible with previous versions.
Next, we have an Overriden onCreate
function. This is what gets called whenever our app's activity is first created.
Note: Android has an odd lifecycle, if you're interested in learning more you can check out this image.
Continuing, we have setContentView(R.layout.activity_main)
. This connects with our activity_main
layout that we were just looking at in Part 3.
Now let's try running this!