- 簡單傳值
//傳值
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.main_activity_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//不傳值
// startActivity(new Intent(MainActivity.this, BActivity.class));
Intent intent = new Intent(MainActivity.this, BActivity.class);
intent.putExtra("name", "張三");
intent.putExtra("age", 12);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart");
}
}
//接收方
public class BActivity extends AppCompatActivity {
private TextView tv;
public TextView getTv() {
return findViewById(R.id.tv);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent bIntent = getIntent();
getTv().setText(String.format("name [%s] age [%d]",bIntent.getStringExtra("name"), bIntent.getIntExtra("age", 1)));
}
}
傳遞Bundle
//傳遞bundle
Bundle bundle = new Bundle();
bundle.putString("name", "李四");
bundle.putInt("age", 23);
Intent intent = new Intent(MainActivity.this, BActivity.class);
intent.putExtra("data", bundle);
startActivity(intent);
Intent bIntent = getIntent();
Bundle bundle = bIntent.getBundleExtra("data");
getTv().setText(String.format("name [%s] age [%d]", bundle.getString("name"), bundle.getInt("age", 0)));
傳遞自定義模型數(shù)據(jù) 方式一 java.io.Serializable系統(tǒng)自動序列化 代碼簡單 但速度慢(不建議使用)
import java.io.Serializable;
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;
}
}
//傳遞自定義模型數(shù)據(jù)
Intent intent = new Intent(MainActivity.this, BActivity.class);
intent.putExtra("User", new User("海賊王", 222));
startActivity(intent);
Intent bIntent = getIntent();
User user = (User) bIntent.getSerializableExtra("User");
getTv().setText(String.format("name [%s] age [%d]", user.getName(), user.getAge()));
傳遞自定義模型數(shù)據(jù) 方式二 Parcelable Android 平臺提供的接口需要手動實現(xiàn)序列化 使用復(fù)雜 但速度快(建議使用)
package com.example.elly.myapplication;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
//public class User implements Serializable {
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;
}
protected User(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(getName());
parcel.writeInt(getAge());
}
}
//傳遞自定義模型數(shù)據(jù)
Intent intent = new Intent(MainActivity.this, BActivity.class);
intent.putExtra("User", new User("海賊王2號", 111));
startActivity(intent);
Intent bIntent = getIntent();
User user = (User) bIntent.getParcelableExtra("User");
getTv().setText(String.format("name [%s] age [%d]", user.getName(), user.getAge()));
獲取Activity 的返回值 (Activity反向傳值)
Intent intent = new Intent(MainActivity.this, BActivity.class);
intent.putExtra("User", new User("海賊王3號", 133));
startActivityForResult(intent, RESULT_FIRST_USER);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
((TextView)findViewById(R.id.textView)).setText(data.getStringExtra("backValue"));
}
Intent bIntent = getIntent();
User user = (User) bIntent.getParcelableExtra("User");
getTv().setText(String.format("name [%s] age [%d]", user.getName(), user.getAge()));
getBackBtn().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//數(shù)據(jù)的載體
Intent intent = new Intent();
intent.putExtra("backValue", ((EditText)findViewById(R.id.editText)).getText().toString());
setResult(RESULT_OK, intent);
finish();
}
});