Android Studio avtivity的传值之获取activity返回的数据

一、介绍:

Android Studio avtivity的传值之获取activity返回的数据_第1张图片

三个方法之间的关系:Android Studio avtivity的传值之获取activity返回的数据_第2张图片


二、实例:

Ⅰ、功能:

实现两个activity (activity1、activity2)返回主activity(Mainactivity)时传值给Mainactivity


Ⅱ、布局:

activity1/activity2

Android Studio avtivity的传值之获取activity返回的数据_第3张图片

 


mainactivity

Android Studio avtivity的传值之获取activity返回的数据_第4张图片


Ⅲ、java代码:

activity1

import androidx.appcompat.app.AppCompatActivity;

public class ACTIVITY1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1_);
        final Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.putExtra("msg", "我来自ACTIVITY1!");
                setResult(RESULT_OK,intent);
                finish();

            }
        });

    }
}

 activity2

public class ACTIVITY2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        final Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.putExtra("msg", "我来自ACTIVITY2!");
                setResult(RESULT_OK, intent);
                finish();
            }
        });


    }
}

mainactivity:

public class MainActivity extends AppCompatActivity {
    private static final int ACTIVITY1 = 1;
    private static final int ACTIVITY2 = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button btnmain1 = (Button) findViewById(R.id.button_main1);
        final TextView textView = (TextView) findViewById(R.id.textViewmain);
       
        btnmain1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ACTIVITY1.class);
                startActivityForResult(intent, ACTIVITY1);
            }
        });
        final Button btnmain2 = (Button) findViewById(R.id.button_main2);
        
        btnmain2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ACTIVITY2.class);
                startActivityForResult(intent, ACTIVITY2);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String msg = data.getStringExtra("msg");
        final TextView textViewmain = (TextView) findViewById(R.id.textViewmain);
        switch (requestCode) {
            case ACTIVITY1:
                if (resultCode == RESULT_OK)
                    textViewmain.setText("数据来自ACTIVITY1:" + msg);
                break;
            case ACTIVITY2:
                if (resultCode == RESULT_OK)
                    textViewmain.setText("数据来自ACTIVITY2:" + msg);
                break;
            default:
        }

    }
}

Ⅳ、⭐⭐⭐实现过程⭐⭐⭐

主界面单机按钮→发送请求码并跳转到界面2→界面2单机返回按钮→界面2提供需要返回的数据和结果码→验证请求码和结果码是否匹配→验证成功→跳回主界面并获取到界面2返回的数据


Ⅴ、AVD模拟:

Android Studio avtivity的传值之获取activity返回的数据_第5张图片Android Studio avtivity的传值之获取activity返回的数据_第6张图片Android Studio avtivity的传值之获取activity返回的数据_第7张图片

 

 

 

你可能感兴趣的:(android,studio,android)