【Android学习笔记】使用Intent传递数据

向下一个活动传递数据

创建一个新的工程TransmitDataTest,工程结构如下:

【Android学习笔记】使用Intent传递数据_第1张图片

 编写MainActivity界面代码:




    

编写FirstActivity界面代码:




    

编写MainActivity类,为按钮添加点击事件,当按钮被点击时,向FirstActivity发送一个带有数据的Intent:

package com.example.transmitdatatest;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                //放入数据
                intent.putExtra("stringValue","20190930");
                intent.putExtra("booleanValue",false);
                intent.putExtra("floatValue",3.1415926f);
                startActivity(intent);
            }
        });
    }
}

 编写FirstActivity类,获取MainActivity传递过来的Intent,取出其中的数据并打印:

package com.example.transmitdatatest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Intent intent =  getIntent();
        String s = intent.getStringExtra("stringValue");
        //第二个参数表示如果键值对不存在,取的默认值
        boolean b = intent.getBooleanExtra("booleanValue",true);
        float f = intent.getFloatExtra("floatValue",-1f);
        Log.v("FirstActivity","stringValue is "+s);
        Log.v("FirstActivity","booleanValue is "+b);
        Log.v("FirstActivity","floatValue is "+f);

    }
}

运行APP,点击按钮1,打印结果如下:

 说明借助Intent传递数据到下一个活动是没有问题的。

返回数据给上一个活动

创建新活动SecondActivity,先编写SecondActivity界面代码:




    

修改MainActivity界面代码,增加一个新的按钮2:




    

修改MainActivity类代码,为按钮2添加点击事件:

package com.example.transmitdatatest;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button_1);
        button2 = findViewById(R.id.button_2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                //放入数据
                intent.putExtra("stringValue","20190930");
                intent.putExtra("booleanValue",false);
                intent.putExtra("floatValue",3.1415926f);
                startActivity(intent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                //参数2:请求码是唯一值即可
                startActivityForResult(intent,1);
            }
        });
    }
}

编写SecondActivity类,发送数据给上一个活动:

package com.example.transmitdatatest;

import androidx.appcompat.app.AppCompatActivity;

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

public class SecondActivity extends AppCompatActivity {

    Button returnDataButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        returnDataButton = findViewById(R.id.button_return_data);
        returnDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("data_return","Hello MainActivity");
                //返回数据给上一个活动
                setResult(RESULT_OK,intent);
                //销毁当前活动
                finish();
            }
        });
    }
}

 接写修改MainActivity类代码,重写onActivityResult()方法,响应从活动2返回的数据:

package com.example.transmitdatatest;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button_1);
        button2 = findViewById(R.id.button_2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                //放入数据
                intent.putExtra("stringValue","20190930");
                intent.putExtra("booleanValue",false);
                intent.putExtra("floatValue",3.1415926f);
                startActivity(intent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                //参数2:请求码是唯一值即可
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode){
            case 1:
                if(resultCode==RESULT_OK){
                    String returnData = data.getStringExtra("data_return");
                    Log.v("MainActivity","return data is "+returnData);
                }
                break;
        }
    }
}

运行APP,先点击MainActivity的按钮2跳转到SecondActivity页面,点击SecondActivity页面中的按钮,此时打印输出如下:

说明MainActivity已经收到了SecondActivity返回的数据。

另外,为了防止用户按下Back键返回上一个活动,最好把SecondActivity中的onBackPressed()也重写下:

package com.example.transmitdatatest;

import androidx.appcompat.app.AppCompatActivity;

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

public class SecondActivity extends AppCompatActivity {

    Button returnDataButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        returnDataButton = findViewById(R.id.button_return_data);
        returnDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("data_return","Hello MainActivity");
                //返回数据给上一个活动
                setResult(RESULT_OK,intent);
                //销毁当前活动
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data_return","Hello MainActivity");
        //返回数据给上一个活动
        setResult(RESULT_OK,intent);
        //销毁当前活动
        finish();
    }
}

补充:MainActivity中的onActivityResult()方法需要在SecondActivity销毁后,才能得到执行。

参考书籍:《第一行代码》

你可能感兴趣的:(Android学习笔记)