Tutorial details

Android beginner tutorial: Part 55 - Explicit Intent basics | App Code for Sale | Preview

Android beginner tutorial: Part 55 - Explicit Intent basics | Android Tutorial

The basics of launching an Activity using an Explicit Intent

Overview PAGE TOP

Today we will learn the basics of launching an Activity using an Explicit Intent.

Activities, as well as other Android components, are invoked using Intent objects. Intents can be used for communication between Activities of one or multiple applications.

There are two types of Intents - explicit and implicit. Explicit Intents refer to an exact, specific component. They usually don't carry much information on their own, but simply launch an Activity or some other internal process. Implicit ones don't have a specified component. They carry information so that the system decides what to run based on the requirements.

Today we'll learn how to launch a second Activity using an Explicit Inetnt object. To call an Activity, we need to provide the name of that Activity, which is done using one of the 3 methods - setComponent(), setClass() or setClassName().

First let's design our application, then establish the Intents. There will be 2 Activities - MainActivity and SecondActivity. The MainActivity layout will have 2 TextViews and a Button. The button will be used for calling the second Activity, the first TextView will be used to indicate that this is the main activity. The second TextView will act as a log, where we will display the name of all callback functions when they are called.

Go to activity_main.xml and use this layout code:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text="Main Activity"
        />

    <Button android:id="@+id/callButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call another activity"
        />

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

</LinearLayout>

Create a new xml file in the layout folder next to activitymain.xml, call it activitysecond.xml.

Display a single TextView there to indicate that it is the second Activity:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text="Second Activity"
        />

</LinearLayout>

Go to the src folder using the PackageExplorer in Eclipse and open the current package. Add a new class there called SecondActivity.java.

The code is very simple, it doesn't even do anything:

package com.kircode.codeforfood_test;

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

public class SecondActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

}

}

And here is the code for MainActivity.java class. In the onCreate() function we set a click event listener to the Button that we have. Inside of the onClick() function of the listener, create a new Intent object, call its setClass() method to refer to the SecondActivity.class object, then start the Activity using startActivity() method and passing the intent object in the parameter.

Then create callback onStart(), onRestart(), onResume(), onPause(), onStop() and onDestroy() functions.

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{

private TextView logText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

logText = (TextView)findViewById(R.id.logText);

final Button btn = (Button)findViewById(R.id.callButton);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
}

public void onStart(){
super.onStart();
logText.append("onStart();\n");
}

public void onRestart(){
super.onRestart();
logText.append("onRestart();\n");
}

public void onResume(){
super.onResume();
logText.append("onResume();\n");
}

public void onPause(){
super.onPause();
logText.append("onPause();\n");
}

public void onStop(){
super.onStop();
logText.append("onStop();\n");
}

public void onDestroy(){
super.onDestroy();
logText.append("onDestroy();\n");
}

}

We are almost done. The only thing left is to go to AndroidManifest.xml file and add a new Activity. Open the Manifest using Eclipse and select the Application tab, scroll down to Application Nodes window and click Add...

Set the name of the application to SecondActivity with the package name and all that jazz, it should look something like this:

com.kircode.codeforfood_test.SecondActivity

Save everything and test the application. You should be now able to launch a second activity from your first one. You'll also see the changes in the log text view.

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-55.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.