转载请注明出处:带返回值的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
}
}
}
}
}