3-AI--Activity间的数据传递

作者: e4e52c116681 | 来源:发表于2018-08-24 12:55 被阅读1次

零、前言

打开FromActivity,通过按钮以返回结果方式打开ToActivity,同时在intent中加入数据,在ToActivity的onCreate方法中使用数据填充到TextView上。按返回按钮,将ToActivity数据传递给FromActivity,在onActivityResult方法中验证返回结果并将数据填充到TextView上。

Activity数据传递.gif

一、Java类

FromActivity.java
public class FromActivity extends AppCompatActivity {

    private static final int DATA_CODE = 0x0001;

    @BindView(R.id.btn_for_result)
    Button mBtnForResult;
    @BindView(R.id.tv_result)
    TextView mTvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_from);
        ButterKnife.bind(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case DATA_CODE:
                if (resultCode == RESULT_OK) {
                    String dataFormTarget = data.getStringExtra("data");

                    Bundle personData = data.getBundleExtra("To");
                    Person person = (Person) personData.get("person");
                    mTvResult.setText("dataFormTarget:" + dataFormTarget
                            + "\nperson:" + person.toString());
                }
                break;
        }

    }

    @OnClick({R.id.btn_for_result})
    public void onViewClicked(View view) {
        Intent intent = new Intent(this, ToActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("person", new Person("form", 23));
        intent.putExtra("from", bundle);
        startActivityForResult(intent, DATA_CODE);
    }
}
ToActivity.java
public class ToActivity extends AppCompatActivity {

    @BindView(R.id.btn_send)
    Button mBtnSend;
    @BindView(R.id.tv_to)
    TextView mTvTo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_target);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        Bundle extra = intent.getBundleExtra("from");
        Person from = (Person) extra.get("person");
        mTvTo.setText(from.toString());

    }

    @OnClick(R.id.btn_send)
    public void onViewClicked() {
        backWithData();
        finish();
    }

    private void backWithData() {
        Person jt = new Person("捷特", 24);

        Intent intent = new Intent();
        intent.putExtra("data", "我是ToActivity的数据");

        Bundle bundle = new Bundle();
        bundle.putSerializable("person", jt);
        intent.putExtra("To", bundle);
        setResult(RESULT_OK, intent);
    }

    /**
     * 重写返回键
     */
    @Override
    public void onBackPressed() {
        backWithData();
        super.onBackPressed();
    }
}
Activity传递数据.png

附录

Person.java
public class Person implements Serializable {

    private String name;
    private int age;

    public Person(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 String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
ac_from.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_for_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="112dp"
        android:layout_marginTop="32dp"
        android:text="StartTargetForResult"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击按钮 获取返回结果"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
ac_to.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="24dp"
        android:text="返回给上一个Activity结果"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结果"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

相关文章

  • 3-AI--Activity间的数据传递

    零、前言 打开FromActivity,通过按钮以返回结果方式打开ToActivity,同时在intent中加入数...

  • Activity间传递数据

    Activity间传递对象需要序列化 Activity //在一个Activity中创建Intent对象,并配置参...

  • Activity间传递数据

    1.正向传递数据  当我们通过startActivity(Intent)来启动Activity的时候,可以利用传入...

  • Flink通信机制

    flink内部通信机制 Operator间的数据传递本地线程数据传递远程线程数据传递同一线程的Operator数据...

  • Vue中组件间传值总结 ------ 2020-05-17

    父子组件间传递数据的方式 1、父组件向子组件传递数据 2、子组件向父组件传递数据 3、父子组件相互传递同一数据的两...

  • Vue组件间数据传递

    前言 总结vue组件间的数据传递 路由传参 父组件传递数据给子组件---props 子组件传递数据给父组件---$...

  • Activity间的数据传递

    1.Activity的概念与Activity的生命周期图 2.Activity的创建流程: 3.Activity间...

  • Actity间的数据传递

    Actity间的数据传递有两种方式,第一种是A传递给B,本文主要介绍的是第二种,FirstActity启动Seco...

  • Vue组件间传递数据

    组件是Vue很强大的一个功能,所以掌握Vue组件之间的信息传递很重要。大致分为三种常见的情况。 父组件向子组件传递...

  • 备忘:Activity间传递数据

    基本类型参数传递Activity间传递一些基本类型的数据可以直接进行传递,比如直接通过intent传递 List对...

网友评论

    本文标题:3-AI--Activity间的数据传递

    本文链接:https://www.haomeiwen.com/subject/ooubiftx.html