Passing intent value to another activity...

In this program we will pass value of a intent to another activity class.This is pretty simple.We have to use putExtra() method to pass the value.
For this we will take two XML layout.One is the main and another is newintent.The XML code for the main is

<?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="@string/hello"
    />
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

Here we take a button.when we press it the value is passed to the NewIntent Activity.We take another XML (newintent)with a TextView.The passed value will be shown in this TextView.The XML code for newintent:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

Now we code the MyValue class.(Notice,by convention we should start a class name with capital letter and a XML name with a small letter.)In this class the value(the string "hello") is given that will be passed.

package com.value.android;

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

public class MyValue extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn = (Button)findViewById(R.id.Button01);
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                String a = "hello";
                Intent intent = new Intent(MyValue.this,NewIntent.class);
                intent.putExtra("extra_value", a);
                startActivity(intent);
            }
           
        });
    }
}

Here in this code we create a object named intent of Intent class.The value of "a" is passed to NewIntent class via putExtra.Now we code the NewIntent class.

package com.value.android;

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

public class NewIntent extends Activity{
   
    public void onCreate(Bundle savedInsatanceState){
        super.onCreate(savedInsatanceState);
        setContentView(R.layout.newintent);
        TextView tv=(TextView)findViewById(R.id.TextView01);
       
        Intent intent = this.getIntent();
       
        String value = intent.getStringExtra("extra_value");
        tv.setText("Carried value is:::"+value);
    }

}

Here the most important things to remember is,we have to register the new Activity class NewIntent in the Android manifest.For this we have to open the manifest,open the Activity tab and there we have to add the NewIntent Activity.Otherwise it will give a runtime error,when the button will be pressed.

Using Intents to launch activities:

we can use Intents to launch the phone's basic activities such as the phone dialer, the browser or search.

these intents are called implicit intents cause you don't specify the activity you want to launch, rather Android determines the proper activity to launch based on the required action. also when the launched activity finisheds its work, the original activity has no information that the launched activity has finished it's work

in this example we create an intent that performs a phone number dial action. we don not specify that we want the dialer activity to launch, rather we specify that we want to dial a number and Android launches the dialer activity to perform this action

consider this activity: consists of a TextView and a button to dial the number in the text view.

<?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="Enter the phone number"
        />
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtNumber"
    android:inputType="phone"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dial"
    android:id="@+id/btnDial"
    />
    </LinearLayout>


This is the XML layout.Now to use the DIAL button we need the following java code...