The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tutorial #8 - Setting Up An Activity

Of course, it would be nice if those “Help” and “About” menu choices that we added in the previous tutorial actually did something.

In this tutorial, we will define another activity class, one that will be responsible for displaying simple content like our help text and “about” details. And, we will arrange to start up that activity when those action bar items are selected. The activity will not actually display anything meaningful yet, as that will be the subject of the next few tutorials.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository.

Step #1: Creating the Stub Activity Class and Manifest Entry

First, we need to define the Java class for our new activity, SimpleContentActivity.

Right-click on your main/ sourceset directory in the project explorer, and choose New > Activity > Empty Activity from the context menu. This will bring up a new-activity wizard:

Android Studio New Activity Wizard
Figure 251: Android Studio New Activity Wizard

Fill in SimpleContentActivity in the “Activity Name” field and uncheck the “Generate Layout File” checkbox. Leave “Launcher Activity” unchecked, and uncheck the “Backwards Compatibility (AppCompat)” checkbox. If the package name drop-down is showing the app’s package name, leave it alone. On the other hand, if the package name drop-down is empty, click on it and choose the app’s package name. Then click on Finish.

At this point, your SimpleContentActivity class should look like:

package com.commonsware.empublite;

import android.app.Activity;
import android.os.Bundle;

public class SimpleContentActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }
}

Step #2: Launching Our Activity

Now that we have declared that the activity exists and can be used, we can start using it.

Go into EmPubLiteActivity and modify onOptionsItemSelected() to add in some logic in the R.id.about and R.id.help branches, as shown below:

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.about:
        Intent i=new Intent(this, SimpleContentActivity.class);
        startActivity(i);

        return(true);

      case R.id.help:
        i=new Intent(this, SimpleContentActivity.class);
        startActivity(i);

        return(true);
    }

    return(super.onOptionsItemSelected(item));
  }

In those two branches, we create an Intent, pointing at our new SimpleContentActivity. Then, we call startActivity() on that Intent. Right now, both help and about do the same thing — we will add some smarts to have them load up different content later in this book.

You will need to add an import for android.content.Intent to get this to compile.

If you run this app in a device or emulator, and you choose either the Help or About menu choices… nothing much appears to happen. In reality, what happens is that our SimpleContentActivity appeared, but empty, as we have not given it a full UI yet.

In Our Next Episode…

The preview of this section took that left turn at Albuquerque.