Six ways to pass values between Activities

1. Use the putExtra of Intent to pass

In the first Activity

//创建Intent对象
Intent intent = new Intent(OneActivity.this,TwoActivity.class);
//设置传递键值对                                                                               
intent.putExtra("data",str);                                                                 
//开启TwoActivity                                                                                        
startActivity(intent);

In the second Activity

//获取Intent对象
Intent intent = getIntent();
//获取传递的值
String str = intent.getStringExtra("data");
//设置值
tv.setText(str);

When intent transfers data, what are the data types that can be transferred?

2. Use Intention's Bundle delivery

In the first Activity

//创建Intent对象
Intent intent = new Intent(MainActivity.this,TwoActivity.class);
//创建Bundle对象用来捆绑传递数据
Bundle bundle = new Bundle();
bundle.putString("String数据", str);
bundle.putInt("Int数据",5);
//把Bundle对象通过putExtra()方法放入intent对象中
intent.putExtra("bun", bundle);
//激活TwoActivity
startActivity(intent);

The second Activity

//获取Bundle对象
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bun");
//获取数据
String str = bundle.getString("String数据");
int a=bundle.getInt("Int数据")
//设置数据
tv.setText(str);
tv.setText(""+a);

When the Bundle transfers data, what are the data types that can be transferred?

  

3. Pass data when using Activity to destroy

In the first Activity

Intent intent = new Intent(MainActivity.this,TwoActivity.class);
//用一种特殊方式开启Activity
startActivityForResult(intent, 11);  //requestCode ==11

//获取传回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode ==11 && resultCode ==3){ 
     String str = data.getStringExtra("data");
     tvOne.setText(str);
  }
}

In the second activity

//设置返回的数据
 Intent intent = new Intent();
 intent.putExtra("data", edtOne.getText().toString().trim());
 setResult(3, intent);  //resultCode==3
 //关闭当前activity
 finish();

4. SharedPreferences transfer data

In the first Activity

SharedPreferences sp = this.getSharedPreferences("info", 1);
//获取sp编辑器
Editor edit = sp.edit();
edit.putString("data", str);
edit.commit();
//创建意图对象
Intent intent = new Intent(MainActivity.this,TwoActivity.class);
//激活意图
startActivity(intent);

In the second Activity

SharedPreferences sp = this.getSharedPreferences("info", 1);
//设置数据
tv.setText(sp.getString("data", ""));

5. Use the serialized object Seriazable

Tools

import java.io.Serializable;
public class DataBean implements Serializable {
    private String name;
    private int age;

    public DataBean() {
    }

    public DataBean(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

The first Activity

DataBean ss=new DataBean("carrie",18);
jump_in_item3.putExtra("数据",ss);

The second Activity

//反序列化数据对象
Serializable se = intent.getSerializableExtra("数据2");
     if(se instanceof DataBean){
      //获取到携带数据的DataBean对象db
       DataBean db = (DataBean) se;
       tv.setText(db.getName());
       tv.setText(""+db.getAge());
        }

6. Use static variables to pass data

The first Activity

Intent intent = new Intent(MainActivity.this,TwoActivity.class);
TwoActivity.name="carrie";
TwoActivity.age=18;
startActivity(intent);

The second Activity

//静态变量
protected static String name;
protected static int age;
tv.setText(name+age);

Guess you like

Origin blog.csdn.net/luoro/article/details/124914396