Activity多级跳转带返回值

我们最常用的就是A、B两个activity跳转传值,有时还会遇到A、B、C三个页面传值,如果A——>B——>C,如何将C页面的数据直接返回给A?

针对这个问题,以前遇到你多次了。为此,觉得有必要记录下来,好了,来用下面的例子说明具体操作

首先新建一个项目,创建A、B、C三个Activity,每个Activity上有一个TextView和一个Button,点击A界面的按钮传值到B界面,点击B界面的按钮传值到C界面,点击C界面的按钮返回值到A界面,最终完成测试(在下面的示例中,暂时只考虑A——>B——>C——>A这样的情况)

MainActivity.java

package com.example.shoewann.activityintent;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private android.widget.Button button;
    private android.widget.TextView textView;
    private final int MAINACTIVITY_REQUEST_CODE=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        this.button.setOnClickListener(this);
        this.textView.setText("我是MainActivity的数据");
    }

    private void initView() {
        this.textView = (TextView) findViewById(R.id.textView);
        this.button = (Button) findViewById(R.id.button);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                starter();
                break;
             default:
                break;
        }
    }

    private void starter() {
        Intent starter = new Intent(this, SecondActivity.class);
        starter.putExtra("first","我是来自MainActivity的数据");
        startActivityForResult(starter,MAINACTIVITY_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==Activity.RESULT_FIRST_USER){

            if (requestCode==MAINACTIVITY_REQUEST_CODE){

                if (data!=null){

                    textView.append("\n"+data.getStringExtra("threeth"));
                }
            }
        }
    }
}

activity_a

SecondActivity.java

package com.example.shoewann.activityintent;

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

public class SecondActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "SecondActivity";
    private android.widget.Button button2;
    private android.widget.TextView textView2;
    private final int SECONDACTIVITY_REQUEST_CODE=2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        initView();
        initData();
    }

    private void initView() {
        this.textView2 = (TextView) findViewById(R.id.textView2);
        this.button2 = (Button) findViewById(R.id.button2);
    }

    public void initData(){
        if (getIntent()!=null){
            textView2.setText(getIntent().getStringExtra("first"));
        }
        this.button2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                starter();
                break;
            default:
                break;
        }
    }
    private void starter() {
        Intent starter = new Intent(this, ThreethActivity.class);
        starter.putExtra("second","我是来自SecondActivity的数据");
        startActivityForResult(starter,SECONDACTIVITY_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==Activity.RESULT_FIRST_USER){

            if (requestCode==SECONDACTIVITY_REQUEST_CODE){

                if (data!=null){

                    setResult(Activity.RESULT_FIRST_USER,data);
                    finish();
                }
            }
        }
    }
}

activity_a_b

ThreethActivity.java

package com.example.shoewann.activityintent;

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


public class ThreethActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "ThreethActivity";
    private android.widget.Button button3;
    private android.widget.TextView textView3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threeth);
        initView();
        initData();
    }
    private void initView() {
        this.textView3 = (TextView) findViewById(R.id.textView3);
        this.button3 = (Button) findViewById(R.id.button3);
    }
    public void initData(){
        if (getIntent()!=null){
            textView3.setText(getIntent().getStringExtra("second"));
        }
        this.button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button3:
                starter();
                break;
            default:
                break;
        }
    }
    private void starter() {
        Intent starter = new Intent();
        starter.putExtra("threeth","我是来自ThreethActivity的数据");
        setResult(Activity.RESULT_FIRST_USER,starter);
        finish();
    }

}

activity_b_c.png

C —> A

activity_c_a.png

以上简单的介绍了Activity多级跳转带返回值的具体方法,如果不足,欢迎指正。

if (本文对您有用) { Pay (请随意¥打赏) } else { Commit feedback (底部评论区提交建议、反馈) } 感谢支持!