How to pass variables from one Activity to another in Android
Think of a screnario where you are required to pass variables from one Activity to another.For example ,variables entered in a simple login activity are passed to another activity that uses the variables to connect to a database.
I will illustrate using code how you can pass variables from one activity to another.
Explanation:
In the above code,I have created Intent object and named it intent.Using intent.putExtra() method i added all the three string values name,email and message to my intent.
Its time now to create next activity and get our variables that we passed.
I will illustrate using code how you can pass variables from one activity to another.
How to pass variables between two activities.
Let have a MainActivity class that has three textfields.These fields,name,email and message texts while be entered.
Below is the MainActivity Code.
package net.techoverload.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public final static String EXTRA_NAME="com.example.myfirstapp.NAME";
public final static String EXTRA_EMAIL="com.example.myfirstapp.EMAIl";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//This method will be called once the user clicks send message button
public void sendText(View view) {
Intent intent = new Intent(this, DisplayActivity.class); //Start the intent
EditText editTextMessage = (EditText) findViewById(R.id.edit_message); //get variables from textviews
EditText editTextName = (EditText) findViewById(R.id.enter_name);
EditText editTextEmail = (EditText) findViewById(R.id.enter_email);
String message = editTextMessage.getText().toString(); //convert to string variables
String name = editTextName.getText().toString();//convert to string variables
String email = editTextEmail.getText().toString();//convert to string variables
intent.putExtra(EXTRA_MESSAGE,message); //put variables in an intent
intent.putExtra(EXTRA_NAME,name);
intent.putExtra(EXTRA_EMAIL,email);
startActivity(intent);
}
}
Explanation:
In the above code,I have created Intent object and named it intent.Using intent.putExtra() method i added all the three string values name,email and message to my intent.
Its time now to create next activity and get our variables that we passed.
package net.techoverload.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
import org.w3c.dom.Text;
public class DisplayActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Intent intent=getIntent();
String name=intent.getStringExtra(MainActivity.EXTRA_NAME);
String email=intent.getStringExtra(MainActivity.EXTRA_EMAIL);
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView nameView=new TextView(this);
nameView.setTextSize(20);
nameView.setText(name);
TextView emailView=new TextView(this);
emailView.setTextSize(20);
emailView.setText(email);
TextView messageView=new TextView(this);
messageView.setTextSize(20);
messageView.setText(message);
ViewGroup layout=(ViewGroup)findViewById(R.id.activity_display);
layout.addView(nameView);
layout.addView(emailView);
layout.addView(messageView);
}
}