Android - data return

When we jump from one page to the second page to perform related operations, when we close the second page, we need to return some data from this page to the first page. We need to use:

 

 

E.g:

The layout file main_activity.xml of the first Activity, add the page layout of the running Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="next"
        android:text="下一页" />

</LinearLayout>

 The layout file second_activity.xml of the second Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入名字"
        />
    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入年龄"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回数据"
        android:onClick="returndata"
        />

</LinearLayout>

Create MainActivity:

package com.example.myapplication55;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private int myrequestCode=111;
    private TextView tv_show;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show = (TextView)findViewById(R.id.tv_show);//获取id
    }

    //下一页按钮的方法
    public void next(View view) {

        Intent intent=new Intent(this,SecondActivity.class);
        startActivityForResult(intent,myrequestCode);// 1.startActivityForResult()方法,用于开启一个Activity
    }
    //3.onActivityResult()方法,接收回传的数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==myrequestCode && resultCode==222){
            String name=data.getStringExtra("my_name");
            int age=data.getIntExtra("my_age",18);
            tv_show.setText(name+"    "+age);
        }
    }
}

Create SecondActivity:

package com.example.myapplication55;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SecondActivity extends AppCompatActivity {

    private EditText et_name;
    private EditText et_age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        et_name = (EditText) findViewById(R.id.et_name);
        et_age = (EditText) findViewById(R.id.et_age);
    }

    //返回数据按钮的方法
    public void returndata(View view) {
        String name=et_name.getText().toString();
        int age=Integer.parseInt(et_age.getText().toString());
        Intent intent=new Intent();
        intent.putExtra("my_name",name);
        intent.putExtra("my_age",age);
        setResult(222,intent);//2. setResult()方法,用于携带数据进行回传
        finish();
    }
}

result:

first run

Click on the next page:

 

 

Click to return data: the second page closes, the first page receives data

 

 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123693717