Android向上一个Activity返回参数

1.简单说明

当下一个Activity收到上一个Activity传来的参数时,我们可能还需要需要向上一个Activity返回一些信息。方法是:

  1. 前一个Activity打包好数据后调用startActivityForResult(Intent intent, int RequestCode)方法,第二个参数标识请求的唯一性。如果对数据打包不清楚的话参照这篇博文
  2. 后一个Activity打包好应答数据后调用setResult(int resultCode, Intent data)返回数据,第一个参数表示接收状态。
  3. 前一个页面重写onActivityResult(int requestCode, int resultCode, @Nullable Intent data)方法。requestCode表示请求编号,resultCode表示处理结果。
    传递参数给下一个页面
    收到后返回消息
    接收到返回的消息

2.简单实现

  1. 发送参数并接受返回参数的Activity
  • activity_send_parameter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".SendParameterActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="姓名"
        />

    <EditText
        android:inputType="number"
        android:maxLength="3"
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="年龄"
        />

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="11"
        android:hint="电话"/>

    <Button
        android:text="传递参数给下一个页面"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_ok"/>

    <TextView
        android:id="@+id/tv_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用于接收返回消息"
        android:textSize="18sp"
        android:textColor="#000000"/>
</LinearLayout>
  • SendParameterActivity.java
package xyz.strasae.androidlearn.my;

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SendParameterActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_phone;
    private EditText et_age;
    private TextView tv_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_parameter);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_phone = findViewById(R.id.et_phone);
        tv_msg = findViewById(R.id.tv_msg);
        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SendParameterActivity.this, ReceiveParameterActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("name", et_name.getText().toString());
                bundle.putInt("age", Integer.valueOf(et_age.getText().toString()));
                bundle.putString("phone", et_phone.getText().toString());
                intent.putExtras(bundle);
                //第二个参数用于表示请求的唯一性
                startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle bundle = data.getExtras();
        tv_msg.setText(String.format("接收到的返回参数如下:\n姓名:%s\n电话:%s\n年龄:%d\n", bundle.getString("name"), bundle.getString("phone"), bundle.getInt("age")));
        Toast.makeText(this, "接收返回参数成功", Toast.LENGTH_SHORT).show();
    }
}
  1. 接收参数并返回参数的Activity
  • activity_receive_parameter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".ReceiveParameterActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_show"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="用于接收上一个页面传来的参数的TextView"
        />
    <Button
        android:id="@+id/btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回消息"/>
</LinearLayout>
  • ReceiveParameterActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

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

public class ReceiveParameterActivity extends AppCompatActivity {
    private TextView tv_show;
    private String name;
    private String phone;
    private int age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_parameter);
        tv_show = findViewById(R.id.tv_show);
        Bundle bundle = this.getIntent().getExtras();
        name = bundle.getString("name");
        phone = bundle.getString("phone");
        age = bundle.getInt("age");
        tv_show.setText(String.format("收到的消息如下:\n姓名:%s\n电话:%s\n年龄:%d", name, phone, age));
        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("name", name);
                bundle.putString("phone", phone);
                bundle.putInt("age", age);
                intent.putExtras(bundle);
                //接收成功还是失败
                setResult(Activity.RESULT_OK, intent);
                //关闭当前页面
                finish();
            }
        });
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值