SoFunction
Updated on 2025-04-06

About 6 ways to pass data between Android activities

putExtra pass using Inten

In the first activity

//Create an intent object Intent intent = new Intent(this,);
 //Set the pass key-value pair ("data",str);
 //Activate Intent startActivity(intent);

In the second activity

// Get the intent object Intent intent = getIntent();
 //Get the passed value String str = ("data");
 //Set the value (str);

Bundle pass using Intention

In the first activity

//Create an intent object Intent intent = new Intent(,);
 //Transfer data using data bundle Bundle bundle = new Bundle();
 ("data", str);
 //Change the data bundle to change the intention ("bun", bundle);
 //Activate Intent startActivity(intent);

The second activity

//Get Bundle Intent intent = getIntent();
 Bundle bundle = ("bun");
 String str = ("data");
 (str);

Passing data when destroyed using Activity

In the first activity

  Intent intent = new Intent(,);
  //Open Activity in a special way startActivityForResult(intent, 11);
//Set data 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 (requestCode, resultCode, data);
 String str = ("data");
 (str);
}

In the second activity

//Set the returned data Intent intent = new Intent();
 ("data", ().toString().trim());
 setResult(3, intent);
 //Close the current activity finish();

SharedPreferences pass data

In the first activity

SharedPreferences sp = ("info", 1);
 //Get the sp editor Editor edit = ();
 ("data", str);
 ();
 //Create an intent object Intent intent = new Intent(,);
 //Activate Intent startActivity(intent);

In the second activity

SharedPreferences sp = ("info", 1);
 //Set data (("data", ""));

Using serialized object Serializable

Tools

import ;
class DataBean implements Serializable {
 private String name;
 private String sex;
 public String getName() {
 return name;
 }
 public void setName(String name) {
  = name;
 }
 public String getSex() {
 return sex;
 }
 public void setSex(String sex) {
  = sex;
 }
}

The first activity

//Creation Intent Intent intent = new Intent(,);
 DataBean bean = new DataBean();
 //Save data into DataBean object through the set method ("La La");
 ("male");
 ("key", bean);
 startActivity(intent);

The second activity

Intent intent = getIntent();
 //Deserialize the data object Serializable se = ("key");
 if(se instanceof DataBean){
  //Get the DataBean object db carrying data  DataBean db = (DataBean) se;
  (()+"==="+());
 }

Passing data using static variables

The first activity

Intent intent = new Intent(,);
  ="Awesome";
  ="you say";
  startActivity(intent);

The second activity

// Static variableprotected static String name;
protected static String str;
(str+name);

The above are 6 ways to transfer data between Android Activity that the editor has introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!