1.在 Activity 之间传递参数

  1. 简单传值
//传值
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)));

传递自定义模型数据 方式一 java.io.Serializable系统自动序列化 代码简单 但速度慢(不建议使用)

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;
    }
}
//传递自定义模型数据
                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()));

传递自定义模型数据 方式二 Parcelable Android 平台提供的接口需要手动实现序列化 使用复杂 但速度快(建议使用)

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 CREATOR = new Creator() {
        @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());
    }
}

 //传递自定义模型数据
                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) {
                //数据的载体
                Intent intent = new Intent();
                intent.putExtra("backValue", ((EditText)findViewById(R.id.editText)).getText().toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });

你可能感兴趣的:(1.在 Activity 之间传递参数)