您的位置:首页 > 其它

带返回值的Intent

2016-12-10 15:24 295 查看
转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53558735

与一般跳转的区别

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: