O'Reilly logo

Android™ Tablet Application Development For Dummies® by Donn Felker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Your First Activity

Now you can create your first activity! Well, technically speaking, you already created your first activity — the MainActivity.java file — when you created a project through the New Android Project wizard in Chapter 5. Therefore, you will not be creating a new activity; you will only be working with the MainActivity.java file you already created. Open this file now.

As you read previously, the entry point into your application is the onCreate() method. The code for your MainActivity.java file already contains an implementation of the onCreate() method. Right now, your code should look like this:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {                →4
        super.onCreate(savedInstanceState);                          →5
        setContentView(R.layout.main);                               →6
    }
}

You will be writing initialization code directly below the setContentView() method shortly.

Some points to note about this code:

→4 This line is the onCreate method, which initiates the activity. The Bundle savedInstanceState part ensures that upon initiation, a bundle containing this activity's previous state (if there was one) gets passed in. This is useful when restarting an activity that has been killed by the system, so information is not lost.

The bundle is a key value that maps between string keys and various parcelable types. A bundle gives you, the developer, a way to pass groups of information back and forth between ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required