Passing Data From One Activity to Another Activity in Android

Introduction

 
Here is my first article about Android on c-sharpcorner.com.
 
The following screen shows what we will build in this tutorial.
 
We will pass a username and a password in the EditView Control and if they match correctly then we need to move to another Intent else show a message box saying it is invalid. Also if the user successfully logins then we will show a Welcome , {username} message  in TextView.
 
So the main intention of this article is to explain how to pass data across multiple intents inside Android. 
 
7-22-2011 10-01-03 AM.gif
 
The main logic used here is very simple, wherein you just need to use the putExtra Method to pass the data across multiple program screens.
 
The putExtra method accepts many kinds of data, such as String, int, float, byte etc..
 
It's much like what we do in an ASP.Net query string; we append it with payload and in the next page we retrieve it with the same name. The same is here; we do putExtra, specifying the name of the variable and we will retrieve it soon, for another intent.
 
Main.Java
  1.  package com.Program2;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10. public class Main extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState)  
  14.     {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         /* Get button Text1 Text2 */  
  18.         final EditText  edUsername  = (EditText) findViewById(R.id.editText1);  
  19.         final EditText  edPassword  = (EditText)findViewById(R.id.editText2);  
  20.         Button btnValidate = (Button)findViewById(R.id.button1);  
  21.         btnValidate.setOnClickListener(new OnClickListener()  
  22.            {  
  23.                 public void onClick(View arg0)  
  24.                 {  
  25.                      String uname  = edUsername.getText().toString();  
  26.                      String pass = edPassword.getText().toString();  
  27.                      if(uname.equals("kirtan") && pass.equals("12345"))  
  28.                      {  
  29.                          Intent intent = new Intent(Main.this,Success.class);  
  30.                          intent.putExtra("username",edUsername.getText().toString());  
  31.                          startActivity(intent);  
  32.                      }  
  33.                      else  
  34.                      {  
  35.                            Toast.makeText(Main.this"Invalid Usename password pair.", Toast.LENGTH_LONG).show();  
  36.                      }  
  37.               }  
  38.            });  
  39.     }  
Success.Java
  1. package com.Program2;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5. public class Success extends Activity  
  6. {  
  7.      protected void onCreate(Bundle savedInstanceState)  
  8.      {  
  9.            // TODO Auto-generated method stub  
  10.            super.onCreate(savedInstanceState);  
  11.            setContentView(R.layout.success);  
  12.            TextView tv = (TextView) findViewById(R.id.textView1);  
  13.            tv.setText("Welcome ,"+getIntent().getExtras().getString("username"));  
  14.      }  
7-22-2011 10-11-36 AM.gif
 


Similar Articles