Switch Among Activities and Send Data in Android

Introduction

 
This article explains how to switch among Activities and send data in Android. Android Studio is used to create the sample.
 
This application will send data to another activity when you enter the data in the previous activity on a button click. For this, you need to create a Java class named MainActivity where you will use two textviews, two edittexts, and a button. So you will enter the data that you want to in another activity on a button click.
 
So to data you need to use an Intent. Intents are used to data and perform switching among activities. So to use Intent methods you will create the object of the intent and the context and activity on which you want to switch as a parameter. Now to transfer data you will call the putExtra() method that contains both the parameters of the string type. the data as a parameter in the putExtra() method and the intent object as a parameter in the startActivity() method.
 
Now create another Java class named Second that holds the sent data by the activity. Create the object of the intent and call the getStringExtra() method that will return the data in string formated by the MainActivity. Now set the data in textview by calling the setText method provided by the TextView class.
 
Step 1
 
Create a project like this:
 
"File" -> "New Project" -> "Android Application"
 
Create Project
 
Step 2
 
Create an XML file named actvitymain.xml as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.    android:background="#FFC6A9"  
  7.     >  
  8.     <TextView android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Enter Name: "/>  
  11.     <EditText android:id="@+id/editText1"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginBottom="10dip"/>  
  15.     <TextView  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="Enter Age: "  
  19.         />  
  20.     <EditText android:id="@+id/editText2"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_marginBottom="10dip"  
  24.         android:inputType="number"/>  
  25.    
  26.     <Button android:id="@+id/sendButton"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="Send"  
  30.         android:layout_marginTop="15dip"/>  
  31. </LinearLayout> 
Step 3
 
Create another XML file with the following contents:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:background="#FFC6A9">  
  8.     <TextView android:id="@+id/textViewName"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_margin="15dip"  
  12.         android:textSize="18dip"/>  
  13.     <TextView android:id="@+id/txtViewAge"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_margin="15dip"  
  17.         android:textSize="18dip"/>  
  18.     <Button android:id="@+id/backButton"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginTop="15dip"  
  22.         android:text="Back"/>  
  23. </LinearLayout> 
Step 4
 
Create a Java class file with the following contents:
  1. package com.jsonparsingexample;   
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. public class MainActivity extends Activity {  
  10.     // Initializing variables  
  11.     EditText Name;  
  12.     EditText Age;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         Name = (EditText) findViewById(R.id.editText1);  
  18.         Age = (EditText) findViewById(R.id.editText2);  
  19.         Button btnNextScreen = (Button) findViewById(R.id.sendButton);  
  20. //Listening to button event  
  21.         btnNextScreen.setOnClickListener(new View.OnClickListener() {  
  22.             public void onClick(View arg0) {  
  23. //Starting a new Intent  
  24.                 Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);  
  25. //Sending data to another Activity  
  26.                 nextScreen.putExtra("Name", Name.getText().toString());  
  27.                 nextScreen.putExtra("Age", Age.getText().toString());  
  28.                 startActivity(nextScreen);  
  29.             }  
  30.         });  
  31.     }  
Step 5
 
Create another Java class file with the following contents:
  1. package com.jsonparsingexample;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9. public class SecondActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.second);  
  15.         TextView txtName = (TextView) findViewById(R.id.textViewName);  
  16.         TextView txtEmail = (TextView) findViewById(R.id.txtViewAge);  
  17.         Button btnBack = (Button) findViewById(R.id.backButton);  
  18.         Intent i = getIntent();  
  19. // Receiving the Data  
  20.         String name = i.getStringExtra("Name");  
  21.         String age = i.getStringExtra("Age");  
  22.         Log.e("Second Screen", name + "." + age);  
  23. // Displaying Received data  
  24.         txtName.setText(name);  
  25.         txtEmail.setText(age);  
  26. // Binding Click event to Button  
  27.         btnBack.setOnClickListener(new View.OnClickListener() {  
  28.             public void onClick(View arg0) {  
  29. //Closing SecondScreen Activity  
  30.                 finish();  
  31.             }  
  32.         });  
  33.     }  
Step 6
 
Add a Second Activity name in the Android manifest.xml file with the following contents:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.jsonparsingexample"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.jsonparsingexample.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     <activity  
  26.         android:name=".SecondActivity"></activity>  
  27.     </application>  
  28.     <uses-permission android:name="android.permission.INTERNET"/>  
  29. </manifest> 
Step 7
 
Output after run Application
 
S
tep 8
 
Enter Data
 
Step 9
 
Output Data in Android


Similar Articles