【Android studio 】( chapter04 .向上一个Activity返回数据)

58 阅读2分钟

chapter04

  • 向上一个Activity返回数据

image.png

  • activity_act_request.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_request"
        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"/>


</LinearLayout>
  • activity_act_response.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_request"
        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"/>


</LinearLayout>
  • ActRequestActivity.java
package com.example.chapter04;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.chapter04.util.DateUtil;

public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener {

    private static  final String mRequest="你睡了吗?";
    private ActivityResultLauncher<Intent> register;
    private TextView tv_response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_request);
        TextView tv_request=findViewById(R.id.tv_request);
        tv_request.setText("待发送的消息为:\n"+mRequest);

        tv_response = findViewById(R.id.tv_response);

        findViewById(R.id.btn_request).setOnClickListener(this);
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if(result!=null) {
                Intent intent = result.getData();
                if(intent!=null&&result.getResultCode()== 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);
                }
            }

        });
    }

    @Override
    public void onClick(View v) {
        //创建一个新包裹
    Intent intent=new Intent(this, ActResponseActivity.class);
    Bundle bundle=new Bundle();
    bundle.putString("request_time", DateUtil.getNowTime());
    bundle.putString("request_content",mRequest);
    intent.putExtras(bundle);
    register.launch(intent);
    }
}
  • ActResponseActivity.java
package com.example.chapter04;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.chapter04.util.DateUtil;

public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String mReponse="没有,再见";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response);
        TextView tv_request=findViewById(R.id.tv_request);
         //从上一个页面传来的意图中获取快递包裹
        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_request.setText(desc);
        findViewById(R.id.btn_response).setOnClickListener(this);

        TextView tv_response = findViewById(R.id.tv_response);
        tv_response.setText("待返回的消息为:"+mReponse);
    }

    @Override
    public void onClick(View v) {
        Intent intent =new Intent();
        Bundle bundle=new Bundle();
        bundle.putString("response_time", DateUtil.getNowTime());
        bundle.putString("response_content",mReponse);
        intent.putExtras(bundle);
        //携带意图返回上一个页面,RESULT_OK表示处理成功
        setResult(Activity.RESULT_OK,intent);
        //结束当前的活动页面
        finish();
    }
}