Android原生开发笔记 向上一个Activity返回数据

java yekong

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

A页面

package com.example.chapter03;

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 android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chapter03.util.DateUtil;
import com.example.chapter03.util.Utils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_request;
    private TextView tv_response;
    private String mRequest = "你好";
    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_request = findViewById(R.id.tv_request);
        tv_response = findViewById(R.id.tv_response);
        tv_request.setText("待发送消息" + mRequest);
        findViewById(R.id.tv_request).setOnClickListener(this);
        findViewById(R.id.btn_request).setOnClickListener(this);

        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null) {
                    Intent intent = result.getData();
                    if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
                        Bundle bundle = intent.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_response.setText(desc);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, TextViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_time", DateUtil.getNowTime());
        bundle.putString("request_content", mRequest);
        intent.putExtras(bundle);
        register.launch(intent);
    }
}

B页面

package com.example.chapter03;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter03.util.DateUtil;
import com.example.chapter03.util.Utils;

public class TextViewActivity extends AppCompatActivity implements View.OnClickListener {
    private String mRequest = "你好2";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        TextView tv_receive = findViewById(R.id.tv_response);
        Bundle bundle = getIntent().getExtras();
        findViewById(R.id.btn_response).setOnClickListener(this);
        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_receive.setText(desc);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, TextViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_time", DateUtil.getNowTime());
        bundle.putString("request_content", mRequest);
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}

喜欢