向上一个Activity返回数据

第一个Activity

<TextView
    android:id="@+id/tv_result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/btn_response"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="传送请求数据"/>
<TextView
    android:id="@+id/tv_response"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
public class ActRequest8 extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_result,tv_response;
    String request = "你吃饭了吗?";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_request8);
        tv_response = findViewById(R.id.tv_response);
        tv_result = findViewById(R.id.tv_result);
        tv_result.setText("待发送的消息为:"+request);
        findViewById(R.id.btn_response).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,ActResponse8.class);
        //创建一个包裹
        Bundle bundle = new Bundle();
        bundle.putString("request_time","2023-4-10");
        bundle.putString("request_content",request);
        intent.putExtras(bundle);
        startActivityForResult(intent,1);
    }
// 第一个参数表示请求码;第二个参数表示回传码,第三个参数表示回传的数据
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    { // 接收返回数据
        super.onActivityResult(requestCode, resultCode,intent);
        if (intent != null && requestCode == 1 && resultCode== Activity.RESULT_OK){
            Bundle bundle = intent.getExtras();
            String  response_time = bundle.getString("response_time");
            String response_content = bundle.getString("response_content");
            String desc = String.format("收到请求消息:\n应答时间为:%s\n应答内容为:%s",response_time,response_content);
            tv_response.setText(desc);
        }
    }
}

第二个Activity

<TextView
    android:id="@+id/tv_result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/btn_response"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="返回应答数据"/>
<TextView
    android:id="@+id/tv_response"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
public class ActResponse8 extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_result,tv_response;
    String response = "我吃过了";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response8);
        tv_result = findViewById(R.id.tv_result);
        tv_response = findViewById(R.id.tv_response);
        Bundle bundle = getIntent().getExtras();
        String request_time = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为:%s\n请求内容为:%s",request_time,request_content);
        tv_result.setText(desc);
        findViewById(R.id.btn_response).setOnClickListener(this);
        TextView tv_response = findViewById(R.id.tv_response);
        tv_response.setText("待返回的消息为:" + response);
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_time","2023-4-11");
        bundle.putString("response_content",response);
        intent.putExtras(bundle);
//        携带意图返回上一个页面,RESULT_OK表示处理成功
        setResult(Activity.RESULT_OK,intent);
        finish();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当当2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值