得到新打开Activity 关闭后返回的数据

这里写图片描述

MainActivity.java

package com.example.actdemo;

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

public class MainActivity extends Activity {

    private static final int OTHER_REQUESTCODE1 = 1;
    private static final int OTHER_REQUESTCODE2 = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void clickView(View v) {

        Intent intent = null;
        Bundle bundle = null;

        switch (v.getId()) {
        case R.id.button1:
            // 进入到下一个activity
            intent = new Intent(this, OtherActivity.class);
            intent.putExtra("name", "chj"); // 存放到intent中

            bundle = new Bundle();
            bundle.putString("name", "chjxxx"); // 存放到bundle中 再存放到意图中
            // 放入意图对象中
            intent.putExtras(bundle);
            // 执行的意图 标识 你什么时候发送的什么样的意图
            startActivityForResult(intent, OTHER_REQUESTCODE1);

            break;
        case R.id.button2:

            // 进入到下一个activity
            intent = new Intent(this, OtherActivity.class);
            intent.putExtra("name", "chj"); // 存放到intent中

            bundle = new Bundle();

            bundle.putString("name", "bcxxxx"); // 存放到bundle中 再存放到意图中
            // 放入意图对象中
            intent.putExtras(bundle);
            // 执行的意图 标识 你什么时候发送的什么样的意图
            startActivityForResult(intent, OTHER_REQUESTCODE2);
            break;
        case R.id.button3:

            // 进入到下一个activity
            intent = new Intent(this, UserActivity.class);

            bundle = new Bundle();

            bundle.putString("name", "hlxxxxx"); // 存放到bundle中 再存放到意图中
            // 放入意图对象中
            intent.putExtras(bundle);
            // 执行的意图 标识 你什么时候发送的什么样的意图
            startActivityForResult(intent, OTHER_REQUESTCODE1);
            break;

        }

    }

    /** * 得到新打开Activity 关闭后返回的数据 * * 请求码与结果码 就是区分 谁发送的请求 谁处理的结果 */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        // 判断你发送的请求 是不是
        if (requestCode == OTHER_REQUESTCODE1) {
            // 来自于这个响应
            if (resultCode == OtherActivity.OTHER_RESULTCODE) {
                // 获取附带数据
                String name = data.getStringExtra("name");
                Toast.makeText(this, "你点击button1的按钮返回的结果" + name, 1).show();
            } else if (resultCode == UserActivity.USER_RESULTCODE) {
                // 获取附带数据
                String name = data.getStringExtra("name");
                Toast.makeText(this, "你点击button1的按钮返回的结果" + name, 1).show();
            }
        } else if (requestCode == OTHER_REQUESTCODE2) {
            // 来自于这个响应
            if (resultCode == OtherActivity.OTHER_RESULTCODE) {
                // 获取附带数据
                String name = data.getStringExtra("name");
                Toast.makeText(this, "你点击的button2的按钮返回的结果" + name, 1).show();
            }
        }
    }
}

OtherActivity.java

package com.example.actdemo;

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

public class OtherActivity extends Activity {

    public static final int OTHER_RESULTCODE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        String name1 = getIntent().getStringExtra("name");// 获取意图中的数据
        Bundle bundle = getIntent().getExtras();
        String name2 = bundle.getString("name");//获取意图中 bundle中的数据 chjxxx 解释
        Toast.makeText(this, name1 + "," + name2, 1).show();

    }

    public void clickView(View v){
        Intent intent  = new Intent();
        intent.putExtra("name", "bc");
        setResult(OTHER_RESULTCODE, intent);
        //杀死当前的activity
        finish();
    }
}

UserActivity.java

package com.example.actdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class UserActivity extends Activity {
    public static final int USER_RESULTCODE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
    }

    public void clickView(View v) {
        Intent intent = new Intent();
        intent.putExtra("name", "xxxxxhlxxxbc");
        setResult(USER_RESULTCODE, intent);
        // 杀死当前的activity
        finish();
    }
}

Activity_main.xml
这里写图片描述

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="80dp"
        android:onClick="clickView"
        android:text="@string/btn_tip" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
         android:onClick="clickView"
        android:text="@string/btn_tip" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="280dp"
        android:onClick="clickView"
        android:text="@string/btn_tip_user" />

</RelativeLayout>

Activity_other.xml
这里写图片描述

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" >

    <Button  android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="190dp" android:onClick="clickView" android:text="@string/btn_close_tip" />

</RelativeLayout>

Activity_user.xml
这里写图片描述

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" >

    <TextView  android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />

    <Button  android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="148dp" android:onClick="clickView" android:text="关闭activity" />

</RelativeLayout>

演示效果:
这里写图片描述
点击相应按钮进入一个新的activity中,该新的activity中会接收到MainActivity传来的值,
这里写图片描述
点击关闭activity按钮,该activity被关闭finish,回到MainActivity中,此时将接受到该activity传过来的值。
这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/u/2411130/blog/477355

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值