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 ... |
Get Android™ Tablet Application Development For Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.