Activity的数据传递(startActivityForResult , onActivityResult , setResult 的用法)

1.startActivityForResult(Intent intent, int requestCode);用法
MainActivity2里面传递一个Intent对象,给即将打开的Mars或Moon,并用requestCode区分打开哪个 Activity

startActivityForResult(Intent intent, int requestCode);
第一个参数Intent intent:  一个Intent对象
第二个参数int requestCode:>= 0,MarsMoon结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回

2.onActivityResult(int requestCode, int resultCode, Intent data);用法
MainActivity2里重写这个方法,接收Mars或Moon传入的Intent的对象,MainActivity2既可以通过switch/case语句用requestCode 来区分不同的Mars或Moon,也可以通过resultCode来区分是否返回成功

onActivityResult(int requestCode, int resultCode, Intent data)
int requestCode: 这个整数requestCode提供给onActivityResult,是以便确认返回的数据是从哪个Activity返回的。这个requestCode  和startActivityForResult中的requestCode相对应。
int resultCode:    由MarsMoon通过其setResult()方法返回。 
Intent data:         一个Intent对象,带有返回的数据。

3.setResult(int resultCode, Intent data);用法
Mars或Moon调用这个方法把数据返回到MainActivity2,同时也返回resultCode判断是否返回成功

setResult(int resultCode, Intent data)
int resultCode:  当MarsMoon结束时resultCode将归还在MainActivity2onActivityResult()中,一般为RESULT_CANCELED , RESULT_OKIntent data:       一个Intent对象,返回给父Activity的数据。

4.例子:
本实例一共有三个Activity。MainActivity2,Mars,Moon
跳转到Mars,把MainActivity2带给Mars的消息显示在Mars中。
点击返回MainActivity2时把Mars带给MainActivity2的消息传到MainActivity2.
同理:
跳转到Moon,把MainActivity2带给Moon的消息显示在Moon中。
点击返回MainActivity2时把Moon带给MainActivity2的消息传到MainActivity2.
MainActivity2➡跳转到Mars Mars➡MainActivity2
MainActivity2➡跳转到Moon Moon➡MainActivity2

下面是MainActivity2类对象

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

    private Button button = null;
    private Button button2 = null;
    private TextView text  = null;
    private static final int Mars  = 0;
    private static final int Moon  = 1;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        text = (TextView) findViewById(R.id.text);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity2.this , Mars.class);
                String content = "MainActivity2来的消息:你好,我是来自MainActivity2上的小老鼠。我好想去你们的Mars呀" ;
                intent.putExtra( "FromEarth", content);
                startActivityForResult(intent, Mars);
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity2.this , Moon.class);
                String content = "MainActivity2来的消息:你好,我是来自MainActivity2上的小老鼠。我好想去你们Moon呀" ;
                intent.putExtra( "FromEarth", content);
                startActivityForResult(intent, Moon);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)//重写onActivityResult方法
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {//判断是否返回成功
            switch (requestCode) {//  requestCode   这个参数用来判断是从哪个Activity返回的
                case Mars:
                    Bundle MarsBuddle = data.getExtras();
                    String MarsMessage = MarsBuddle.getString("FromMars");
                    text.setText(MarsMessage);
                    break;
                case Moon:
                    Bundle MoonBuddle = data.getExtras();
                    String MoonMessage = MoonBuddle.getString("FromMoon");
                    text.setText(MoonMessage);
                    break;
            }
        }
    }
}

activity_main2的布局文件

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myapplication001.MainActivity2">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#03A9F4"
        android:text="跳转到Mars  "
        android:textColor="#F3EEEE" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:background="#03A9F4"
        android:text="跳转到Moon"
        android:textColor="#F4F2F2" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:gravity="center"
        android:textSize="34sp" />

Mars类对象


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Mars extends AppCompatActivity {

    private Button button = null;
    private TextView textView =null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mars);
        Intent EarthIntent = getIntent();
        String EarthMessage = EarthIntent.getStringExtra("FromEarth" );
        textView = (TextView) findViewById(R.id.textView);
        textView.setText(EarthMessage);
        button = (Button) findViewById(R.id.button3);
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                String passString = "Mars来的消息:Hello,我是Mars的Jack,非常高兴你能来Mars" ;
                intent.putExtra( "FromMars", passString);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

activity_mars布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Mars">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textSize="24sp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#03A9F4"
        android:text="返回"
        android:textColor="#F8F3F3" />
</RelativeLayout>

Moon类对象

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Moon extends AppCompatActivity {

    private Button button = null;
    private TextView textView =null;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moon);
        Intent EarthIntent = getIntent();
        String EarthMessage = EarthIntent.getStringExtra("FromEarth" );
        textView = (TextView) findViewById(R.id.textView2);
        textView.setText(EarthMessage);
        button = (Button) findViewById(R.id.button4);
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                String passString = "Moon来的消息:Hello,我是Moon的Lucy,非常欢迎你来Moon" ;
                intent.putExtra( "FromMoon", passString);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

activity_moon布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Moon">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textSize="24sp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="false"
        android:background="#03A9F4"
        android:text="返回"
        android:textColor="#F8F6F6" />
</RelativeLayout>

以下是app运行截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值