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

简介: 零、前言打开FromActivity,通过按钮以返回结果方式打开ToActivity,同时在intent中加入数据,在ToActivity的onCreate方法中使用数据填充到TextView上。

零、前言

打开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>

后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
公众号.jpg
相关文章
|
2月前
|
人工智能 算法 数据可视化
AI Earth ——开发者模式案例2:Landsat系列影像数据去云
AI Earth ——开发者模式案例2:Landsat系列影像数据去云
33 3
|
29天前
|
机器学习/深度学习 存储 人工智能
为什么AI处理私有数据,需要使用向量数据库
大语言模型通过概率和向量数据库查询来生成高质量内容,当预测概率低于阈值时,利用相似性从本地数据中获取信息,向量数据库通过向量化、表示、查询、搜索和解码等步骤,帮助模型处理未知数据。
|
3月前
|
人工智能 弹性计算 自然语言处理
【Hello AI】AIACC-ACSpeed性能数据
本文展示了AIACC-ACSpeed的部分性能数据,相比较通过原生DDP训练模型后的性能数据,使用AIACC-ACSpeed训练多个模型时,性能具有明显提升。
|
3月前
|
机器学习/深度学习 人工智能 安全
【论文速递】CSET - 小数据的大AI潜力
【论文速递】CSET - 小数据的大AI潜力
17 0
|
7天前
|
机器学习/深度学习 数据采集 人工智能
|
8天前
|
人工智能 Cloud Native 算法
数据之势丨AI时代,云原生数据库的最新发展趋势与进展
AI与云数据库的深度结合是数据库发展的必然趋势,基于AI能力的加持,云数据库未来可以实现更快速的查询和决策,帮助企业更好地利用海量数据进行业务创新和决策优化。
数据之势丨AI时代,云原生数据库的最新发展趋势与进展
|
2月前
|
人工智能 开发者
AI Earth ——开发者模式案例8:利用Landsat-8数据进行地表温度反演
AI Earth ——开发者模式案例8:利用Landsat-8数据进行地表温度反演
29 0
|
2月前
|
机器学习/深度学习 人工智能 自然语言处理
数据标注是AI认识世界的起点
数据标注是AI认识世界的起点
55 2
数据标注是AI认识世界的起点
|
3月前
|
人工智能 算法 关系型数据库
PolarDB for AI助力菜鸟实现一站式数据智能
菜鸟通过应用PolarDB for AI,实现云资源成本降低了50%
160 2
|
3月前
|
人工智能 弹性计算 固态存储
【Hello AI】AIACC-AGSpeed性能数据
本文展示了AIACC-AGSpeed(简称AGSpeed)的部分性能数据,相比较通过PyTorch原生Eager模式训练模型后的性能数据,使用AGSpeed训练多个模型时,性能具有明显提升。

热门文章

最新文章