Wednesday, August 5, 2009

Exercise: Intent & Bundle

In the previous exercise, I implemented one activity with two layout. In this exercise, I implement the same application, with same function and presentation, using two activity, HelloAndroid.java and HelloAndroid_2.java. The activity is switched using Intent, the data is passed using Bundle.

We can keep both layout, main.xml and main2.xml, no change.

We have to re-write HelloAndroid.java to two separated files, HelloAndroid.java and HelloAndroid_2.java.

HelloAndroid.java

package com.example.helloandroid3;

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

public class HelloAndroid extends Activity {

private
Button okButton;
Button cancel1Button;
EditText textName;
EditText textPhonenumberIs;
EditText textEmailIs;
EditText textWebsiteIs;
EditText textAddressIs;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

startLayout1();
}

private Button.OnClickListener okOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
textName = (EditText) findViewById(R.id.whoareyou);
CharSequence textName_value = textName.getText();

textPhonenumberIs = (EditText) findViewById(R.id.phonenumberIs);
CharSequence textPhonenumberIs_value = textPhonenumberIs.getText();

textEmailIs = (EditText) findViewById(R.id.emailIs);
CharSequence textEmailIs_value = textEmailIs.getText();

textWebsiteIs = (EditText) findViewById(R.id.websiteIs);
CharSequence textWebsiteIs_value = textWebsiteIs.getText();

textAddressIs = (EditText) findViewById(R.id.addressIs);
CharSequence textAddressIs_value = textAddressIs.getText();

Intent intent = new Intent();
intent.setClass(HelloAndroid.this, HelloAndroid_2.class);

Bundle bundle = new Bundle();
bundle.putCharSequence("bName", textName_value);
bundle.putCharSequence("bPhonenumber", textPhonenumberIs_value);
bundle.putCharSequence("bEmail", textEmailIs_value);
bundle.putCharSequence("bWebsite", textWebsiteIs_value);
bundle.putCharSequence("bAddress", textAddressIs_value);

intent.putExtras(bundle);

startActivity(intent);
finish();
}
};

private Button.OnClickListener cancelOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
};

private void startLayout1(){
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(okOnClickListener);
cancel1Button = (Button) findViewById(R.id.cancel_1);
cancel1Button.setOnClickListener(cancelOnClickListener);
};
}



HelloAndroid_2.java

package com.example.helloandroid3;

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

public class HelloAndroid_2 extends Activity {

private
Button backButton;
Button cancel2Button;
TextView nameField;
TextView phonenumberField;
TextView emailField;
TextView websiteField;
TextView addressField;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

startLayout2();

Bundle bundle = this.getIntent().getExtras();
CharSequence textName_value = bundle.getCharSequence("bName");
nameField = (TextView) findViewById(R.id.name);
nameField.setText("Hello "+textName_value);

CharSequence textPhonenumberIs_value = bundle.getCharSequence("bPhonenumber");
phonenumberField = (TextView) findViewById(R.id.phonenumber);
phonenumberField.setText("Phone Number: "+textPhonenumberIs_value);

CharSequence textEmailIs_value = bundle.getCharSequence("bEmail");
emailField = (TextView) findViewById(R.id.email);
emailField.setText("Email: "+textEmailIs_value);

CharSequence textWebsiteIs_value = bundle.getCharSequence("bWebsite");
websiteField = (TextView) findViewById(R.id.website);
websiteField.setText("Website: "+textWebsiteIs_value);

CharSequence textAddressIs_value = bundle.getCharSequence("bAddress");
addressField = (TextView) findViewById(R.id.address);
addressField.setText("Address: "+textAddressIs_value);
};

private Button.OnClickListener backOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(HelloAndroid_2.this, HelloAndroid.class);

startActivity(intent);
finish();
}
};

private Button.OnClickListener cancelOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
};

private void startLayout2(){
setContentView(R.layout.main2);
backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(backOnClickListener);
cancel2Button = (Button) findViewById(R.id.cancel_2);
cancel2Button.setOnClickListener(cancelOnClickListener);
};
}


Because we have two activity, so we have to modify the file AndroidManifest.xml, to include the additional activity.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid3"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloAndroid"
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=".HelloAndroid_2"></activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>


The project files can be downloaded here.

4 comments:

Sesso said...

Thanks!

Levi91 said...

Thanks a lot, it's helpfull :)

Anonymous said...

Thanks

Anonymous said...

Thank you for the simple yet effective example