• 【Android】-- 向上个和下个Activity页面发送数据


     Bundle

    在代码中发送消息包裹,调用意图对象的putExtras方法,即可存入消息包裹。

    在代码中接收消息包裹,调用意图对象的getExtras方法,即可取出消息包裹。

    例:

    1. //创建一个包裹
    2. Bundle bundle = new Bundle();
    3. bundle.putString("request_content",tv_send.getText().toString());
    4. intent.putExtras(bundle);

     

    一、向下一个Activity发送数据

    Intent使用Bundle对象存放待传递的数据信息。

    Bundle对象操作各类型数据的读写,方法说明如下:

    例:

    发送页面的xml文件

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_send"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"
    9. android:text="这是要发送的文字"/>
    10. <Button
    11. android:id="@+id/btn_send"
    12. android:layout_width="match_parent"
    13. android:layout_height="wrap_content"
    14. android:gravity="center"
    15. android:text="发送文字"/>
    16. </LinearLayout>

    java类

    1. public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {
    2. private TextView tv_send;
    3. @Override
    4. protected void onCreate(Bundle savedInstanceState) {
    5. super.onCreate(savedInstanceState);
    6. setContentView(R.layout.activity_act_send);
    7. tv_send = findViewById(R.id.tv_send);
    8. findViewById(R.id.btn_send).setOnClickListener(this);
    9. }
    10. @Override
    11. public void onClick(View view) {
    12. Intent intent = new Intent(this,ActReceiveActivity.class);
    13. //创建一个包裹
    14. Bundle bundle = new Bundle();
    15. bundle.putString("request_content",tv_send.getText().toString());
    16. intent.putExtras(bundle);
    17. startActivity(intent);
    18. }
    19. }

     接收页面的xml文件

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_receive"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content" />
    9. </LinearLayout>

    java类

    1. public class ActReceiveActivity extends AppCompatActivity {
    2. private TextView v_receive;
    3. @Override
    4. protected void onCreate(Bundle savedInstanceState) {
    5. super.onCreate(savedInstanceState);
    6. setContentView(R.layout.activity_act_receive);
    7. v_receive = findViewById(R.id.tv_receive);
    8. //从上一个页面传来的意图中获取包裹
    9. Bundle bundle = getIntent().getExtras();
    10. String request_content = bundle.getString("request_content");
    11. String desc = String.format("收到请求消息:\n内容为:%s",request_content);
    12. v_receive.setText(desc);
    13. }
    14. }

     

     

     

    二、向上一个Activity返回数据

    处理下一个页面的应答数据,详细步骤说明如下:

    • 上一个页面打包好请求数据,调用startActivityForResult方法执行跳转动作。
    • 下一个页面接收并解析请求数据,进行相应处理
    • 下一个页面在返回上一个页面时,打包应答数据并调用setResult方法返回数据包裹。
    • 上一个页面重写方法onActivityResult,解析获得下一个页面的返回数据。

    例:

    请求页面xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_request"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content" />
    9. <Button
    10. android:id="@+id/btn_request"
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content"
    13. android:gravity="center"
    14. android:text="传送请求数据"/>
    15. <TextView
    16. android:id="@+id/tv_response"
    17. android:layout_width="match_parent"
    18. android:layout_height="wrap_content" />
    19. </LinearLayout>

    java类

    1. public class ActResquestActivity extends AppCompatActivity implements View.OnClickListener {
    2. private static final String mRequest = "i'm here";
    3. private ActivityResultLauncher<Intent> register;
    4. private TextView tv_response;
    5. @Override
    6. protected void onCreate(Bundle savedInstanceState) {
    7. super.onCreate(savedInstanceState);
    8. setContentView(R.layout.activity_act_resquest);
    9. TextView tv_request = findViewById(R.id.tv_request);
    10. tv_request.setText("待发送的信息:"+mRequest);
    11. tv_response = findViewById(R.id.tv_response);
    12. findViewById(R.id.btn_request).setOnClickListener(this);
    13. register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
    14. if(result != null){
    15. Intent intent = result.getData();
    16. if(intent != null && result.getResultCode() == Activity.RESULT_OK){
    17. Bundle bundle = intent.getExtras();
    18. String response_content = bundle.getString("response_content");
    19. String desc = String.format("收到返回消息:%s",response_content);
    20. tv_response.setText(desc);
    21. }
    22. }
    23. });
    24. }
    25. @Override
    26. public void onClick(View view) {
    27. Intent intent =new Intent(this,ActResponseActivity.class);
    28. //创建一个包裹
    29. Bundle bundle = new Bundle();
    30. bundle.putString("request_content",mRequest);
    31. intent.putExtras(bundle);
    32. register.launch(intent);
    33. }
    34. }

    应答页面xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_request"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content" />
    9. <Button
    10. android:id="@+id/btn_response"
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content"
    13. android:gravity="center"
    14. android:text="返回应答数据"/>
    15. <TextView
    16. android:id="@+id/tv_response"
    17. android:layout_width="match_parent"
    18. android:layout_height="wrap_content" />
    19. </LinearLayout>

    java类

    1. public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {
    2. private static final String mresponse = "over";
    3. @Override
    4. protected void onCreate(Bundle savedInstanceState) {
    5. super.onCreate(savedInstanceState);
    6. setContentView(R.layout.activity_act_response);
    7. TextView tv_request = findViewById(R.id.tv_request);
    8. Bundle bundle = getIntent().getExtras();
    9. String request_content = bundle.getString("request_content");
    10. String desc = String.format("收到请求:%s",request_content);
    11. tv_request.setText(desc);
    12. findViewById(R.id.btn_response).setOnClickListener(this);
    13. TextView tv_response = findViewById(R.id.tv_response);
    14. tv_response.setText("待返回消息:"+mresponse);
    15. }
    16. @Override
    17. public void onClick(View view) {
    18. Intent intent = new Intent();
    19. Bundle bundle = new Bundle();
    20. bundle.putString("response_content",mresponse);
    21. intent.putExtras(bundle);
    22. //携带意图返回上一页面,RESULT_OK表示处理成功
    23. setResult(Activity.RESULT_OK,intent);
    24. //结束页面
    25. finish();
    26. }
    27. }

     

     


  • 相关阅读:
    【GlobalMapper精品教程】023:Excel数据通过相同字段连接到属性表中(气温降水连接到气象台站)
    [附源码]Python计算机毕业设计Django财务管理系统
    人员定位系统如何构建企业安全防护体系?
    模型部署 利用Tensorflow Serving部署模型
    开发Chrome插件,实现网站自动登录
    「哈哥赠书活动 - 48期」-『商业分析思维与实践:用数据分析解决商业问题宣传文案』
    ajax图书管理项目
    java计算机毕业设计沙县小吃点餐系统源程序+mysql+系统+lw文档+远程调试
    一种辅助自律的方式:暑假学习生活打卡的数据分析及思考
    RFID仓库管理系统解决方案有哪些功能模块
  • 原文地址:https://blog.csdn.net/Tir_zhang/article/details/126948186