Posts tagged Bundle
Android: Passing data between Activities
5With the Android framework, data can be passed between different Activities by adding it to the Intent object before starting the new Activity, and then retrieving it using a Bundle object. We will demonstrate this by creating a small demo with two Activities, the first activity, TextInputActivity will allow the user to type in some text and tap a button. Tapping the button will open ViewTextActivity, taking the inputted text with it in the Intent, this text will be retrieved and displayed in ViewTextActivity.
This demo will cover the method for primitive data types, we’ll cover passing your own data types/objects in another post.
First we will get the layouts out the way, both are very simple.
text_input_layout.xml
<?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" android:orientation="vertical"> <EditText android:id="@+id/the_input" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/the_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to next Activity" android:onClick="goToNextActivity"/> </LinearLayout>
text_view_layout.xml
<?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:id="@+id/the_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt"/> </LinearLayout>
Now we’ll create TextInputActivity, this is declared as the main activity in the AndroidManifest.xml. In here we’ll add the onClick handler, goToNextActivity() that’s referenced in the xml layout. Within this method, first we’ll get a reference to the EditText and extract the text from it to a String. Then we create a new Intent object to ViewTextActivity. We’ll call Intent.putExtra() to add the String to the Intent. the first parameter is the name we’ll use to retrieve it later – this is a constant that we’ll declare in ViewTextActivity so don’t worry about the error for now. We’ll just need to remember to add this constant and give it public access in TextViewActivity. Finally, call startActivity() passing it our Intent to start the new Activity.
TextInputActivity.java
package samcoles.bundleexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class TextInputActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_input_layout);
}
public void goToNextActivity(View v) {
String theText = null;
//first get the EditText View and extract the text...
EditText theInput = (EditText)findViewById(R.id.the_input);
if(theInput != null) {
theText = theInput.getText().toString();
}
//create a new Intent
Intent i = new Intent(this, ViewTextActivity.class);
//add theText to the intent
i.putExtra(ViewTextActivity.TEXT_DATA, theText);
//start the activity
startActivity(i);
}
}
Next we’ll add our second Activity, ViewTextActivity. Remember to add this to your AndroidManifest.xml or your app will crash!
First we’ll define the public constant TEXT_DATA that we used in TextInputActivity.
public static final String TEXT_DATA = "textData";
Then in the overridden onCreate() method we’ll get a reference to the Bundle object in the Intent by calling getIntent().getExtras(). We’ll then call Bundle.getString(), passing the TEXT_DATA constant to retrieve our value. Finally we’ll get a reference to the TextView in our layout and call TextView.setText() passing our String.
ViewTextActivity.java
package samcoles.bundleexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ViewTextActivity extends Activity {
public static final String TEXT_DATA = "textData";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_view_layout);
Bundle extras = getIntent().getExtras();
if(extras != null) {
String theText = extras.getString(TEXT_DATA);
if(theText != null) {
TextView t = (TextView)findViewById(R.id.the_text);
if(t != null) {
t.setText(theText);
}
}
}
}
}
Here’s a screenshot of each activity in the demo in action:
