Recipe 1.1 Linking Activities

Android Versions
Level 1 and above
Permissions
None
Source Code to Download at Wrox.com
Linking.zip

Unless you are writing a Hello World application, chances are good that your application contains several activities that you need to connect in order to form a cohesive application. This recipe shows you the various ways to link to another activity in your Android application.

Solution

Suppose you have two activities in your application. The following AndroidManifest.xml file shows the two activities classes, MainActivity and Activity2:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.learn2develop.linking"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".Activity2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="net.learn2develop.Activity2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>       

    </application>

</manifest>

Assuming you are currently in the MainActivity ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps 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.