Starting an Activity

The simplest way one activity can start another is with the startActivity method:

    public void startActivity(Intent intent)

You might guess that startActivity(Intent) is a static method that you call on the Activity subclass that you want to start. But it is not. When an activity calls startActivity(Intent), this call is sent to the OS.

In particular, it is sent to a part of the OS called the ActivityManager. The ActivityManager then creates the Activity instance and calls its onCreate(Bundle) method, as shown in Figure 5.7.

Figure 5.7  Starting an activity

Illustration shows starting an activity.

How does the ActivityManager know which Activity to start? That information is in the Intent parameter.

Communicating with intents

An intent is an object that a component can use to communicate with the OS. The only components you have seen so far are activities, but there are also services, broadcast receivers, and content providers.

Intents are multipurpose communication tools, and the Intent class provides different constructors depending on what you are using the intent to do.

In this case, you are using an intent to tell the ActivityManager which activity to start, so you will use this constructor:

    public Intent(Context packageContext, Class<?> cls)

The Class argument specifies the activity class that the ActivityManager should start. The Context argument tells the ActivityManager which application package the activity class can be found in (Figure 5.8).

Figure 5.8  The intent: telling ActivityManager what to do

Illustration shows starting an activity.

Within mCheatButton’s listener, create an Intent that includes the CheatActivity class. Then pass the intent into startActivity(Intent) (Listing 5.7).

Listing 5.7  Starting CheatActivity (QuizActivity.java)

mCheatButton = (Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Start CheatActivity
        Intent intent = new Intent(QuizActivity.this, CheatActivity.class);
        startActivity(intent);
    }
});

Before starting the activity, the ActivityManager checks the package’s manifest for a declaration with the same name as the specified Class. If it finds a declaration, it starts the activity, and all is well. If it does not, you get a nasty ActivityNotFoundException, which will crash your app. This is why all of your activities must be declared in the manifest.

Run GeoQuiz. Press the CHEAT! button, and an instance of your new activity will appear on screen. Now press the Back button. This will destroy the CheatActivity and return you to the QuizActivity.

Explicit and implicit intents

When you create an Intent with a Context and a Class object, you are creating an explicit intent. You use explicit intents to start activities within your application.

It may seem strange that two activities within your application must communicate via the ActivityManager, which is outside of your application. However, this pattern makes it easy for an activity in one application to work with an activity in another application.

When an activity in your application wants to start an activity in another application, you create an implicit intent. You will use implicit intents in Chapter 15.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.45