第七课:返回子活动结果(基于AndroidStudio3.2)
在上一节中,我们发起了一个子活动,我们传递了一些数据。 在这我们将看一下如何从子活动中返回数据。下图显示了如何解决这个问题的事件顺序。
我们将从MainActivity启动子活动。 这可以通过创建一个来管理显式的intent对象并调用startActivityForResult。 活动可能会启动多个其他活动,每个活动都可以返回一些结果。 当这些结果回来时,它们都将在onActivityResult方法中,所以我们需要知道从哪个活动每个结果都来自; REQUEST_CODE将帮助我们解决这个问题。
Intent intent = new Intent(MainActivity.this, SubActivity.class);
startActivityForResult(intent, REQUEST_CODE)
当运行时解析intent时,SubActivitygets将被创建并将成为可见。 到那时,它可以通过用户输入创建自己的数据。 如果它想
将数据返回给MainActivity,它需要创建一个intent对象将数据发送回去MainActivity通过捎带意图对象。
String data = "Data to send back";
intent.putExtra("key", data);
setResult(Activity.RESULT_OK, intent);
finish();
当SubActivity调用finish方法时,它将被销毁,并且MainActivity然后将返回到活动堆栈的顶部。 运行时将调用MainActivity’s
onActivityResult;这是我们可以提取SubActivitysent的数据的地方。
一、建立演示的工程和UI
1、创建新项目
名称为GetResultsSubActivity
建立子活动SecondActivity
2、添加相应控件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="40dp"
android:gravity="center"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="294dp"
android:text="Launch 2nd Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".SecondActivity">
<EditText
android:id="@+id/editText"
android:layout_width="215dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="27dp"
android:ems="10"
android:hint="Type something"
android:inputType="text"
android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="301dp"
android:text="Close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
先简单测试ok
二、代码实现
package com.example.administrator.getresultssubactivity;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//The request code can be any value; we just need to mark each activity that will send data back to us
private static final int REQUEST_CODE = 1000;
Button b;
TextView t;
// This will contain all our initialization codes
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button);
t = (TextView) findViewById(R.id.textView);
b.setOnClickListener(this);
}
//When the button is clicked, we will launch SecondActivity
@Override
public void onClick(View v) {
//This creates just the usual explicit intent object
Intent intent = new Intent(this, SecondActivity.class);
/*
The startActivity method will simply launch another screen; we are using the
startActivityForResult method because we expect the target activity to return some data to
us. The request code will serve as a marker that we can use later when the result comes back
to us, and we can use that value to route program logic, in case our application starts several
activities
*/
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*
We check if the request code is from an activity we’re interested in. Also, we need to check if the
result code is RESULT_OK, which means the operation has succeeded. Otherwise, you might
need to add, at the very least, some logging codes here
*/
if((requestCode == REQUEST_CODE) && (resultCode == Activity.RESULT_OK)) {
//We pull the data using the key “secondactivity”; this is the same key that we used back in
//SecondActivity, when we injected the data into the intent
t.setText(data.getStringExtra("secondactivity"));
}
}
}
package com.example.administrator.getresultssubactivity;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
EditText e = (EditText) findViewById(R.id.editText);
String data = e.getText().toString();
//We inject the contents of the data variable into the intent object using the putExtra method. We’re
//assigning it a key of “secondactivity”, and we need to use the same key when we retrieve it later
intent.putExtra("secondactivity", data);
//This will prepare the data we are about to return
setResult(Activity.RESULT_OK, intent);
//This will kill SecondActivity. As soon as it dies, the runtime will call MainActivity’s onActivityResult
finish();
}
});
}
}
运行ok