带返回值的Intent

转载请注明出处带返回值的Intent_Mr_Leixiansheng的博客-CSDN博客

与一般跳转的区别

1、startActivityForResult(intent, 1);

2、

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    reData = data.getStringExtra("data_return");

                }
        }
    }

3、setResult(RESULT_OK, intent);实现跳转的活动和界面

/*
带返回值的Intent
 */
package com.example.administrator.intent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    String  reData; //接收返回数据
    TextView textView;

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

        textView = (TextView) findViewById(R.id.text_view);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, IntentActivity.class);    //跳转设置
                startActivityForResult(intent, 1);          //带返回的跳转  requestCode = 1

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    reData = data.getStringExtra("data_return");

                }
        }
    }

    //活动生命周期
    @Override
    protected void onRestart() {
        super.onRestart();
        textView.setText(reData);       //显示返回值
    }
}

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

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button" />

    <TextView
        android:id="@+id/text_view"
        android:text="返回的数据"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

跳转后的活动和界面

package com.example.administrator.intent;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/12/10.
 */

public class IntentActivity extends AppCompatActivity {

    String reData = "Hello World!";

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

        TextView textView = (TextView) findViewById(R.id.text_view);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                //返回数据
                intent.putExtra("data_return", reData);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

}

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

    <Button
        android:id="@+id/button"
        android:text="结束"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Kotlin写法:

package com.leixiansheng.kotlintest

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.intent_layout.*

/**
 * Created by Leixiansheng on 2018/11/15.
 */
class IntentActivity : AppCompatActivity() {

    private val reData = "Hello World!";


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.intent_layout)

        button.setOnClickListener{
            val intent = Intent()
            intent.putExtra("data_return", reData)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }
}
package com.leixiansheng.kotlintest

import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            val intent = Intent(this, IntentActivity::class.java)
            startActivityForResult(intent, 1)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            1 ->{
                if (resultCode == Activity.RESULT_OK) {
                    val reData = data?.getStringExtra("data_return") ?: ""  //为空则返回“”
                    text_view.text = reData
                }
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值