SIGN UP MEMBER LOGIN:    
ARTICLE

Passing Data From One Activity to Another Activity in Android

Posted by Kirtan Patel Articles | Android Programming July 22, 2011
This article shows how you can pass data from one intent to another intent when calling it from a button in Android
Reader Level:
Download Files:
 

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 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

package com.Program2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState)
    {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
main);

        /* Get button Text1 Text2 */
        final EditText  edUsername  = (EditText) findViewById(R.id.editText1);
        
final EditText  edPassword  = (EditText)findViewById(R.id.editText2);
        Button btnValidate = (Button)findViewById(R.id.button1);
        btnValidate.setOnClickListener(
new OnClickListener()
           {
                
public void
 onClick(View arg0)
                {
                     String uname  = edUsername.getText().toString();
                     String pass = edPassword.getText().toString();

                     if(uname.equals("kirtan") && pass.equals("12345"))
                     {
                         Intent intent = 
new Intent(Main.this,Success.class);
                         intent.putExtra("username",edUsername.getText().toString());
                         startActivity(intent);
                     }
                     
else
                     {
                           Toast.makeText(Main.
this"Invalid Usename password pair.", Toast.LENGTH_LONG).show();
                     }
              }
           });
    }
}

Success.Java

package
 com.Program2;
import android.app.Activity;
import android.os.Bundle;
import
 android.widget.TextView;

public class Success extends Activity
{
     
protected void onCreate(Bundle savedInstanceState)
     {
           
// TODO Auto-generated method stub
           super
.onCreate(savedInstanceState);
           setContentView(R.layout.
success);
           TextView tv = (TextView) findViewById(R.id.
textView1);
           tv.setText(
"Welcome ,"+getIntent().getExtras().getString("username"));
     }
}

7-22-2011 10-11-36 AM.gif
  

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, Its working properly for me. Thanks

Posted by Manoj Pal Apr 20, 2012

Hi Kirtan, Thanks for the article. I think it is worth mentioning there are many ways of doing this. For instance: Complex structures implementing Parcelable/Serializable, SharedPreferences... Nice work. Regards, Nic Roche

Posted by Nic Roche Jul 25, 2011

Thanks Sir :) school is not completed yet still one year left to complete MCA ;-)

Posted by Kirtan Patel Jul 25, 2011

Good stuff. Is your school done?

Posted by Mahesh Chand Jul 25, 2011

congrats on your very first article, you are a best coder that I know. Keep it up!!

Posted by Abhimanyu Kumar Vatsa Jul 23, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor