Android Tutorial: Creating Another Activity

The main activity of an Android application is started by the system itself when the user selects the app icon from the Home screen. In an application with multiple activities, it is possible (and easy) to start another activity. In fact, starting an activity from another activity can be done simply by calling the startActivity method like this. Next article: Android Tutorial: Activity Related Intents

Android Another Activity

startActivity(intent);

where the intent is an instance of the android.content.Intent class.

As an example, consider the SecondActivityDemo project that accompanies this book. It has two activities, MainActivity and SecondActivity. MainActivity contains a button that when clicked starts SecondActivity. This project also shows how you can write an event listener programmatically.

The manifest for SecondActivityDemo

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.secondactivitydemo" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <application 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity
            android:name="com.example.secondactivitydemo.MainActivity" 
            android:label="@string/app_name" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN"/> 
                <category android:name="android.intent.category.LAUNCHER"/> 
            </intent-filter> 
        </activity> 
        <activity
            android:name="com.example.secondactivitydemo.SecondActivity" 
            android:label="@string/title_activity_second" > 
        </activity> 
    </application>
</manifest>

Unlike the previous application, this project has two activities, one of which is declared as the main activity.

The layout files for the main and second activities are listed in Listings.

The activity_main.xml file

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/first_screen" /> 

</RelativeLayout>

The activity_second.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".SecondActivity" > 

    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

</RelativeLayout>

Both activities contain a TextView. Touching the TextView in the main activity starts the second activity and pass a message to the latter. The second activity displays the message in its TextView.

The activity class for the main activity is given.

The MainActivity class

package com.example.secondactivitydemo;
import android.app.Activity;
import android.content.Intent; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent; 
import android.view.View;
import android.view.View.OnTouchListener; 
import android.widget.TextView; 

public class MainActivity extends Activity implements 
        OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.textView1); 
        tv.setOnTouchListener(this);
    }

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it 
        // is present. 
        getMenuInflater().inflate(R.menu.menu_main, menu); 
        return true; 
    } 

    @Override 
    public boolean onTouch(View arg0, MotionEvent event) { 
        Intent intent = new Intent(this, SecondActivity.class); 
        intent.putExtra("message", "Message from First Screen"); 
        startActivity(intent); 
        return true; 
    } 
}

To handle the touch event, the MainActivity class has implemented the OnTouchListener interface and overridden its onTouch method. In this method, you create an Intent and put a message in it. You then call the startActivitymethod to start the second activity.

The SecondActivity class is given.

The SecondActivity class

package com.example.secondactivitydemo;
import android.app.Activity;
import android.content.Intent; 
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView; 

public class SecondActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_second); 
        Intent intent = getIntent(); 
        String message = intent.getStringExtra("message"); 
        ((TextView) findViewById(R.id.textView1)).setText(message); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.menu_second, menu); 
        return true; 
    } 
}

In the onCreate method of SecondActivity, you set the view content as usual. You then call the getIntent method and retrieve a message from its getStringExtra method, which you then pass to the setText method of the TextView. You retrieve the TextView by calling the findViewById method.

The main activity and the second activity are shown in figures 1.1 & 1.2.

Figure 1.1: The main activity in SecondActivityDemo

Figure 1.2: The second activity in SecondActivityDemo

 

Leave a Reply

Your email address will not be published.