一个Activity向另外一个Activity传递值的方法

时间:2022-07-22
本文章向大家介绍一个Activity向另外一个Activity传递值的方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一:使用Intent来向下一活动传值

1.关键代码 假如FirstActivity要向SecondActivity传递值

//FristActivity
String data = "Hello SecondActivity";
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("maple",data);//第一个参数是键值,第二个参数是被传递的值
startActivity(intent);
//SecondActivity
Intent intent = getIntent();
String data = intent.getStringExtra("maple");

2.具体实现代码 1)FirstActivity:

public class FirstActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button)findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(FirstActivity.this,"You clicked Button 1",Toast.LENGTH_SHORT).show();
                String data = "Hello SecondActivity!";
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("maple",data);
                startActivity(intent);

                //finish();
            }
        });
        button1.setText("走你");

    }
}

2)SecondActivity:

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.Toast;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Button button2 = (Button)findViewById(R.id.button_2);
        Intent intent = getIntent();
        String data = intent.getStringExtra("maple");
        Toast.makeText(this,data,Toast.LENGTH_SHORT).show();
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
    }
}

二:通过Intent结合Bundle来传递数据

1.关键代码 假如FirstActivity要向SecondActivity传递值

Intent intent = new Intent();
Bundle bundle = new Bundle();
intent.setClass(FirstActivity.this,SecondActivity.class);
String data = "Hello SecondActivity";
bundle.putString("maple",data)//第一个参数是键值,第二个参数是待传递的内容
intent.putExtras(bundle);//把bundle与intent绑定
startActivity(intent);
//SecondActivity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String data = bundle.getString("maple");

2.完整代码 1)FirstActivity:

import android.content.Intent;
import android.content.UriMatcher;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button)findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(FirstActivity.this,"You clicked Button 1",Toast.LENGTH_SHORT).show();
                
                String data = "Hello SecondActivity!";
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("maple",data);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        button1.setText("走你");

    }

2)SecondActivity:

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.Toast;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Button button2 = (Button)findViewById(R.id.button_2);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String data = bundle.getString("maple");
        Toast.makeText(this,data,Toast.LENGTH_SHORT).show();
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
    }
}

三:利用Intent实现下一活动向上一活动传值

Activity中有一个startActivityForResult()方法,这个方法期望在活动销毁的时候能够返回一个结果给上一活动。 1.关键代码:

//FirstActivity
//1)
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent,1);/*第一个参数是Intent的实例,第二个参数是requestCode。
requestCode和resultCode一起配合用来验证是不是我们所需要的那个活动传回来的值。resultCode在onActivityResult(int requestCode, int resultCode, Intent data)方法中出现。*/
//2)重新覆写方法onActivityResult()
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    String returnedData = data.getStringExtra("maple");
                    Toast.makeText(this,returnedData,Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
//SecondActivity
Intent intent = new Intent();
String data = "Hello FirstActivit";
intent.putExtra("maple",data);
setResult(RESULT_OK,intent);//第一个参数是requestCode,第二个是用来传值的intent的实例

2.完整代码 1)FirstActivity

public class FirstActivity extends AppCompatActivity {


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    String returnedData = data.getStringExtra("maple");
                    Toast.makeText(this,returnedData,Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button)findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(FirstActivity.this,"You clicked Button 1",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);
            }
        });
        button1.setText("走你");

    }

2)SecondActivity

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Button button2 = (Button)findViewById(R.id.button_2);
        Intent intent = new Intent();//只用来携带被传递的值,不跳转活动
        String data = "Hello FirstActivity";
        intent.putExtra("maple",data);
        setResult(RESULT_OK,intent);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
    }
}

四:结果演示

一 、二的结果:

image.png

image.png

三的结果:

image.png

image.png