Android activity之间值的传递

activity之间值的传递需要用到Intent对象,然后将值绑定到Intent对象中进行传递

简单的值传递


比如就传一个值,可以直接将值放入到Intent对象中,比如在mainActivity中有一个button,点击button打开另一个activity,并把值传递过去。

1
2
3
4
5
6
7
8
9
10
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,TheAty.class);
//传递简单数据
intent.putExtra("data","this is hello");
startActivity(intent);

}
});

上面的new Intent(MainActivity.this,TheAty.class) 中TheAty是另一个activity,startActivity(intent)方法打开新的activity,在这之前调用intent.putExtra(“data”,”this is hello”)方法将字符串传递过去,putExtra()方法可以放入各种不同的参数,这里用的放入字符串~

接下来看看如何接受传过来的参数

1
2
3
4
5
6
7
8
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_aty);

Intent intent = getIntent();
textView = findViewById(R.id.tv);
textView.setText(intent.getStringExtra("data"));
}

从上面代码可以看出 使用getIntent()方法获取传递过来的Intent对象,然后使用intent.getStringExtra(“data”)方法来获取传递过来的字符串

复杂的数据传递


对于复杂数据的传递 可以使用Bundle对象来绑定多个参数 然后放入Intent对象进行传递,然后接受的时候通过调用intent.getExtras()方法来接受Bundle对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,TheAty.class);


//传递bundle
Bundle bundle = new Bundle();
bundle.putString("name","lily");
bundle.putInt("age",20);
bundle.putString("name2","coco");
// intent.putExtras(bundle);
intent.putExtra("data",bundle);
startActivity(intent);

}
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_aty);

Intent intent = getIntent();
textView = findViewById(R.id.tv);


//Bundle bundle = intent.getExtras();
Bundle bundle = intent.getBundleExtra("data");
textView.setText(String.format("name=%s,age=%d,name2=%s",
bundle.getString("name"),
bundle.getInt("age"),
bundle.getString("name2","luck")));//name2 不存在的话,可以设置默认值 luck

}

从上面可以看出首先新建Bundle对象,然后放入多个值,然后使用intent.putExtras(bundle)或者intent.putExtra(“data”,bundle)方法进行传递,在接受时使用相应的方法intent.getExtras()或者intent.getBundleExtra(“data”)方法进行接受Bundle

值对象的传递


对象传递有两种方式,一种是对象实现Serializable接口,然后进行传递,另一种方法是实现Parcelable接口,前者是java自带的接口,使用简单,但是效率比后者相对要低

  • Serializable接口方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class User implements Serializable {


private String name;
private int age;

public User(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;
}
}
1
2
3
4
5
6
7
8
9
10
11

findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,TheAty.class);
//传递值对象
intent.putExtra("user",new User("hey",22));
startActivity(intent);

}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_aty);

Intent intent = getIntent();
textView = findViewById(R.id.tv);

User user = (User) intent.getSerializableExtra("user");



textView.setText(String.format("user info:{name=%s,age=%d}"
,user.getName(),user.getAge()));

}

上面代码看出User类实现了Serializable接口,传递的时候使用intent.putExtra(“user”,new User(“hey”,22))方法,接受的时候调用intent.getSerializableExtra(“user”)方法进行接受

  • Parcelable接口方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class User implements Parcelable {

private String name;
private int age;

public User(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;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString("name",getName());
bundle.putInt("age",getAge());
dest.writeBundle(bundle);
}

public static final Creator<User> CREATOR = new Creator<User>(){
@Override
public User createFromParcel(Parcel source) {
Bundle bundle = source.readBundle();
return new User(bundle.getString("name"),bundle.getInt("age"));
}

@Override
public User[] newArray(int size) {
return new User[size];
}
};



}
1
2
3
4
5
6
7
8
9
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,TheAty.class);
//传递值对象
intent.putExtra("user",new User("hey",22));
startActivity(intent);
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_aty);

Intent intent = getIntent();
textView = findViewById(R.id.tv);
User user = intent.getParcelableExtra("user");


textView.setText(String.format("user info:{name=%s,age=%d}"
,user.getName(),user.getAge()));

}

上面的代码可以看出User实现Parcelable接口,需要实现describeContents()方法,writeToParcel(Parcel dest, int flags)两个方法,具体要我们实现的是writeToParcel(Parcel dest, int flags)这个方法,将对象的值都写入到Parcel里面。另外还要定义一个Creator对象,并实现createFromParcel(Parcel source)方法,从Parcel里面获取对象值并返回User对象,另外实现newArray(int size)方法,返回对象数组。

打开的activity将值进行返回

这里也就是打开的activity将值返回到mainActivity,在打开的activty TheAtv增加一个按钮和输入框,点击按钮后将输入框的值返回给mainActivity。

  • TheAtv 代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_aty);

    Intent intent = getIntent();
    textView = findViewById(R.id.tv);
    User user = intent.getParcelableExtra("user");
    textView.setText(String.format("user info:{name=%s,age=%d}"
    ,user.getName(),user.getAge()));


    editText = findViewById(R.id.editText);//输入框
    findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent i = new Intent();
    i.putExtra("data",editText.getText().toString());
    setResult(1,i);
    finish();
    }
    });

    }

Intent不仅用来打开Activity,也可以直接用来传递参数,如上面的按钮点击事件中,新建了Intent对象,并将输入框的值放入Intent对象上,在调用setResult()方法,第一个参数代表resultCode,第二参数代表Intent对象,然后调用finish()方法结束当前activity

  • MainActivity接受参数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this,TheAty.class);
    //传递值对象
    intent.putExtra("user",new User("hey",22));
    //startActivity(intent);
    startActivityForResult(intent,101);
    }
    });

在打开TheAtv时就不能使用startActivity()方法,而要使用 startActivityForResult(intent,101)方法,第一参数代表intent对象,第二个值代表请求代码 requestCode,并且MainActivity中重写onActivityResult(int requestCode, int resultCode, @Nullable Intent data)进行接受返回的值

1
2
3
4
5
6
7
8
9
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(101==requestCode && 1==resultCode){
textView.setText(data.getStringExtra("data"));
}

}

以上就是activity之间参数的传递的方法。