Make a project with the name MessagePassingBetweenActivities
1)create main class as CallingActivity.java and copy following code to it
package jitesh.example.messagepassingbetweenactivities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CallingActivity extends Activity {
public static final int CALLED_ACTIVITY = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button nextActivity = (Button) findViewById(R.id.button1);
nextActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(CallingActivity.this, CalledActivity.class);
Bundle b = new Bundle();
EditText firstName = (EditText) findViewById(R.id.firstName);
EditText lastName = (EditText) findViewById(R.id.lastName);
EditText age = (EditText) findViewById(R.id.age);
b.putString("firstName", firstName.getText().toString());
b.putString("lastName", lastName.getText().toString());
b.putInt("age", Integer.parseInt(age.getText().toString()));
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivityForResult(intent,CALLED_ACTIVITY);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
EditText firstName = (EditText) findViewById(R.id.firstName);
firstName.setText(bundle.getString("firstName"));
EditText lastName = (EditText) findViewById(R.id.lastName);
lastName.setText(bundle.getString("lastName"));
EditText age = (EditText) findViewById(R.id.age);
age.setText(Integer.toString(bundle.getInt("age")));
break;
}
}
}
}
2) the main.xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:gravity="center"
android:text="Calling Activity"
android:textSize="24.5sp" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Name" />
<EditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Call Next Activity" />
</LinearLayout>
</LinearLayout>
3) make a java class with name CalledActivity.java and copy the given code to it
package jitesh.example.messagepassingbetweenactivities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalledActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
getParameters();
Button nextActivity = (Button) findViewById(R.id.button1);
nextActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle b = new Bundle();
EditText firstName = (EditText) findViewById(R.id.firstName2);
EditText lastName = (EditText) findViewById(R.id.lastName2);
EditText age = (EditText) findViewById(R.id.age2);
b.putString("firstName", firstName.getText().toString());
b.putString("lastName", lastName.getText().toString());
b.putInt("age", Integer.parseInt(age.getText().toString()));
//Add the set of extended data to the intent and start it
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK,intent);
finish();
}
});
}
public void getParameters() {
if (CalledActivity.this.getIntent().getExtras() != null)
{
Bundle bundle = this.getIntent().getExtras();
EditText firstName = (EditText) findViewById(R.id.firstName2);
firstName.setText(bundle.getString("firstName"));
EditText lastName = (EditText) findViewById(R.id.lastName2);
lastName.setText(bundle.getString("lastName"));
EditText age = (EditText) findViewById(R.id.age2);
age.setText(Integer.toString(bundle.getInt("age")));
}
}
}
4)make another.xml and should look like this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical" >
<TextView
android:id="@+id/textView8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:gravity="center"
android:text="Called Activity"
android:textSize="24.5sp" />
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
<EditText
android:id="@+id/firstName2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Name" />
<EditText
android:id="@+id/lastName2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="@+id/age2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to Previous Activity" >
</Button>
</LinearLayout>
</LinearLayout>
do not forget to register the activity CalledActivity at manifest. use the given line there!!
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jitesh.example.messagepassingbetweenactivities"
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=".CallingActivity"
android:label="@string/title_activity_calling" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><activity android:name=".CalledActivity"></activity>
</application>
</manifest>
1)create main class as CallingActivity.java and copy following code to it
package jitesh.example.messagepassingbetweenactivities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CallingActivity extends Activity {
public static final int CALLED_ACTIVITY = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button nextActivity = (Button) findViewById(R.id.button1);
nextActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(CallingActivity.this, CalledActivity.class);
Bundle b = new Bundle();
EditText firstName = (EditText) findViewById(R.id.firstName);
EditText lastName = (EditText) findViewById(R.id.lastName);
EditText age = (EditText) findViewById(R.id.age);
b.putString("firstName", firstName.getText().toString());
b.putString("lastName", lastName.getText().toString());
b.putInt("age", Integer.parseInt(age.getText().toString()));
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivityForResult(intent,CALLED_ACTIVITY);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
EditText firstName = (EditText) findViewById(R.id.firstName);
firstName.setText(bundle.getString("firstName"));
EditText lastName = (EditText) findViewById(R.id.lastName);
lastName.setText(bundle.getString("lastName"));
EditText age = (EditText) findViewById(R.id.age);
age.setText(Integer.toString(bundle.getInt("age")));
break;
}
}
}
}
2) the main.xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:gravity="center"
android:text="Calling Activity"
android:textSize="24.5sp" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Name" />
<EditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Call Next Activity" />
</LinearLayout>
</LinearLayout>
3) make a java class with name CalledActivity.java and copy the given code to it
package jitesh.example.messagepassingbetweenactivities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalledActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
getParameters();
Button nextActivity = (Button) findViewById(R.id.button1);
nextActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle b = new Bundle();
EditText firstName = (EditText) findViewById(R.id.firstName2);
EditText lastName = (EditText) findViewById(R.id.lastName2);
EditText age = (EditText) findViewById(R.id.age2);
b.putString("firstName", firstName.getText().toString());
b.putString("lastName", lastName.getText().toString());
b.putInt("age", Integer.parseInt(age.getText().toString()));
//Add the set of extended data to the intent and start it
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK,intent);
finish();
}
});
}
public void getParameters() {
if (CalledActivity.this.getIntent().getExtras() != null)
{
Bundle bundle = this.getIntent().getExtras();
EditText firstName = (EditText) findViewById(R.id.firstName2);
firstName.setText(bundle.getString("firstName"));
EditText lastName = (EditText) findViewById(R.id.lastName2);
lastName.setText(bundle.getString("lastName"));
EditText age = (EditText) findViewById(R.id.age2);
age.setText(Integer.toString(bundle.getInt("age")));
}
}
}
4)make another.xml and should look like this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical" >
<TextView
android:id="@+id/textView8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:gravity="center"
android:text="Called Activity"
android:textSize="24.5sp" />
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
<EditText
android:id="@+id/firstName2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Name" />
<EditText
android:id="@+id/lastName2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="@+id/age2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to Previous Activity" >
</Button>
</LinearLayout>
</LinearLayout>
do not forget to register the activity CalledActivity at manifest. use the given line there!!
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jitesh.example.messagepassingbetweenactivities"
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=".CallingActivity"
android:label="@string/title_activity_calling" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><activity android:name=".CalledActivity"></activity>
</application>
</manifest>
No comments:
Post a Comment