How to Use a Context to Start a Specific Activity in Your Android App

By Barry Burd

One way to start a specific activity in your Android app is with a context. A context is an “interface to global information about an application environment.” (Sayeth the Android docs.) Informally, a context is the background information that you might want to know about the things that are actually happening.

For an Android app, the Context Java object might include the app’s package name, the theme, the wallpaper, the names of files associated with the app, and pointers to location services, to user account services, and to other info. All this stuff is available programmatically by way of a Java Context object.

The word programmatically describes something that you can access (and maybe even modify) in your project’s Java code.

An Android activity runs in a certain context. That makes sense. But here’s an idea that’s difficult to embrace: An activity is a context. It’s a context for two reasons (one being technical; the other being somewhat intuitive):

In the Android SDK, the class android.app.Activity is a subclass of android.content.Context.

An activity has all the things that any context has — namely, the app’s package name, the theme, the wallpaper, the names of files associated with the app, and pointers to location services, to user account services, and other info.

In the code below, an activity calls another activity within the same application.

package my.pack;
import android.app.Activity;
import android.os.Bundle;
public class SomeActivity extends Activity {
// … code of some sort belongs here
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
}

In this code, the last two statements are really saying, “With this activity’s own context, start running an instance of OtherActivity.” (If all goes well, the class OtherActivity extends Android’s Activity class, and you’re good to go.)

In the example, the Intent class’s constructor takes two parameters — a context and a Java class. The word this represents the enclosing SomeActivity instance. That’s good, because the constructor’s first parameter is of type Context, and Android’s Activity class is a subclass of the abstract Context class.

In the example code above, Intent constructor gets the OtherActivitys package name from this — the SomeActivity object’s context.

Each activity is part of an application, and an Application instance is also a context. So in many programs, you can use any of the following method calls (instead of this) to obtain a Context instance:

getContext()
getApplicationContext()
getBaseContext()

The getApplicationContext and getBaseContext methods have limited, specialized uses in Android programs.

In the code below, an activity from one app uses a context to call another app’s activity.

package my.pack;
import android.app.Activity;
import android.os.Bundle;
public class SomeActivity extends Activity {
// … code of some sort belongs here
try {
otherContext =
createPackageContext(“other.pack”,
Context.CONTEXT_IGNORE_SECURITY |
Context.CONTEXT_INCLUDE_CODE);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Class<?> otherClass = null;
try {
otherClass = otherContext.getClassLoader().
loadClass(“other.pack.OtherAppActivity”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Intent intent = new Intent(otherContext, otherClass);
startActivity(intent);
}

The second set of code is more complicated than the first example you see here. But most of the complexity comes from the way Java loads classes. One way or another, this code creates an intent from a context and a class name, and then starts the intent’s activity.