Anda di halaman 1dari 9

Mobile Application Development

Topic: Tutorial on Activity Switch & Intent

Prepared by Mr. Muhammad Nabeel


Android Intent and Intent Filter
Intent:
An Android Intent is an abstract description of an operation to be performed.

It can be used with startActivity to launch an Activity, broadcast Intent to send it to any
interested BroadcastReceiver components, and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with a background Service.

"The intent itself, an Intent object, is a passive data structure holding an abstract description
of an operation to be performed."

Intent-Filter:
Specifies the types of intents that an activity, service, or broadcast receiver can respond to. An
intent filter declares the capabilities of its parent component — what an activity or service can
do and what types of broadcasts a receiver can handle. It opens the component to receiving
intents of the advertised type, while filtering out those that are not meaningful for the
component.

Most of the contents of the filter are described by its <action>, <category>, and <data>
subelements.

Switch Between Screen and Data parsing

In Android user interface is displayed through an activity. In Android app development you
might face situations where you need to switch between one Activity (Screen/View) to another.
In this tutorial I will be discussing about switching between one Activity to another and sending
data between activities.

Opening new Activity:


To open new activity following startActivity() or startActivityForResult() method will be used.

Intent i = new Intent(getApplicationContext(), SecondScreen.class);


StartActivity(i);
Sending parameters to new Activity:

To send parameter to newly created activity putExtra() method will be used.

i.putExtra("key", "value");

// Example of sending email to next screen as


// Key = 'email'
// value = 'myemail@gmail.com'
i.putExtra("email", "myemail@gmail.com");

Receiving parameters on new Activity:

To receive parameters on newly created activity getStringExtra() method will be used.

Intent i = getIntent();
i.getStringExtra("key");

// Example of receiving parameter having key value as 'email'


// and storing the value in a variable named myemail
String myemail = i.getStringExtra("email");

Opening new Activity and expecting result:

In some situations you might expect some data back from newly created activity. In that
situations startActivityForResult() method is useful. And once new activity is closed you should
you use onActivityResult() method to read the returned result.

Intent i = new Intent(getApplicationContext(), SecondScreen.class);


startActivityForResult(i, 100); // 100 is some code to identify the returning result

// Function to read the result from newly created activity


@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){

// Storing result in a variable called myvar


// get("website") 'website' is the key value result data
String mywebsite = data.getExtras().get("result");
}

Sending result back to old activity when StartActivityForResult() is used

Intent i = new Intent();


// Sending param key as 'website' and value as 'androidhive.info'
i.putExtra("website", "abc.info");

// Setting resultCode to 100 to identify on old activity


setResult(100,in);

Closing Activity:

To close activity call finish() method

finish();

Code to Implement for switching Activity

1) Step 1: Create a new Empty Project named it "Switch_Activity"

2) Create a main file named it as Activity_Main.java (If not created already)

3) Create XML file and named it as activity_main.xml (if not created already)

4) Open activity_main.xml and add following code in a file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name: "/>
<EditText android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email: "
/>
<EditText android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"/>
<Button android:id="@+id/btnNextScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send to Next Screen"
android:layout_marginTop="15dip"/>
</LinearLayout>

5) Open Main_Activity.java and add following code into it

Important Note: Do not remove your package name in a file that is written on top of the files.

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

// Initializing variables
EditText inputName;
EditText inputEmail;

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

inputName = (EditText) findViewById(R.id.name);


inputEmail = (EditText) findViewById(R.id.email);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);

//Listening to button event


btnNextScreen.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {


//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), SecondScreenActivity.class);

//Sending data to another Activity


nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("email", inputEmail.getText().toString());

Log.e("n", inputName.getText() + "." + inputEmail.getText());

startActivity(nextScreen);

}
});

}
}

6) Create Screen2.xml and add following code in it.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You Entered..."
android:textSize="25dip"
android:gravity="center"
android:layout_margin="15dip"/>

<TextView android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dip"
android:textSize="18dip"/>

<TextView android:id="@+id/txtEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dip"
android:textSize="18dip"/>

<Button android:id="@+id/btnClose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="Close"/>

</LinearLayout>
7) Create SecondScreenActivity.java and add following code in it.

Important Note: Do not remove your package name in a file that is written on top of the files.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
* Created by Hp on 9/15/2016.
*/

public class SecondScreenActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);

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


TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
Button btnClose = (Button) findViewById(R.id.btnClose);

Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String email = i.getStringExtra("email");
Log.e("Second Screen", name + "." + email);

// Displaying Received data


txtName.setText(name);
txtEmail.setText(email);

// Binding Click event to Button


btnClose.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {


//Closing SecondScreen Activity
finish();
}
});

8) Open AndroidManifest.xml and add following code before end of application tag.

<activity android:name=".SecondScreenActivity"> </activity>

9) Run the program you will see following output.

Anda mungkin juga menyukai