Android pass data between activities

In your Android application, you might have more than one activity. Passing data between activities can be a common task to be performed. In Android, you can achieve this goal by using Intent with the startActivity method. The startActivity method accepts an Intent object. When you create an Intent object, you will specify the Activities that are the sender and receiver. You can use putExtra method to specify the data to be sent from the sender to receiver. Below are different versions of putExtra method that are commonly used:


  putExtra(String name, boolean value)
putExtra(String name, boolean[] value)
putExtra(String name, byte value)
putExtra(String name, byte[] value)
putExtra(String name, char value)
putExtra(String name, char[] value)
putExtra(String name, double value)
putExtra(String name, double[] value)
putExtra(String name, float value)
putExtra(String name, float[] value)
putExtra(String name, int value)
putExtra(String name, int[] value)
putExtra(String name, String value)
putExtra(String name, String[] value)


If you want to send an ArrayList object that contain integer numbers or string, you will use the putIntegerArrayListExtra(String name, ArrayList<Integer> value) or putStringArrayListExtra(String name, ArrayList<String> value) rather than the putExtra method.

Now you will create a new Android. Then modify the activity_main.xml file to add one EditText and one Button as shown below. The EditText allows you to input text and the Button is pushed to send the text in the EditText to another activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >
  <EditText
     android:id="@+id/txtfield"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:hint="Enter your name"
      />
  <Button
      android:id="@+id/btgo"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:onClick="goTo"
      android:layout_gravity="center"
      android:text="Go"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
       />  
  
</LinearLayout>


Then, you will create another Activity (File->New->Other->Android Activity) to receive the data. The name of the new Activity is TestActivity. Open its layout file (activity_test.xml) and add the id attribute to the TextView component so that you can make a reference to it later in code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".TestActivity" >

   <TextView
     android:id="@+id/txtview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/hello_world" />

</RelativeLayout>



In the MainActivity class, you need to create a method called goTo. This method will be called to send data when you click the button.

  public void goTo(View view){
   Intent intent=new Intent(this, TestActivity.class);
   EditText txt=(EditText)findViewById(R.id.txtfield);
intent.putExtra("yourname",txt.getText().toString());
startActivity(intent);


   }

In the TestActivity class, to get the data sent from the MainActivity, you must use the getIntent method to get Intent object from the sender. Then use the getExtras method of the Intent object to get the data. The data is stored in Bundle object. The getString(String name) method is used to take data from the Bundle object. If you sent integer data, you will use the getInt method; If you sent boolean data, you will use the getBoolean.

  package com.example.andtip;

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

public class TestActivity extends Activity {

String yourname;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
//get name sent from another activity
Intent intent=getIntent();
Bundle b=intent.getExtras();
yourname=b.getString("yourname");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}

public void onStart(){
super.onStart();
//show the name in the TextView
TextView txtview=(TextView)findViewById(R.id.txtview);
txtview.setText(yourname);
}

}


Android-pass-data-between-activities

Posted by: Dara | post date: 05-24-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.