如何在Android中将值从一个活动传递到另一个活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3510649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:32:23  来源:igfitidea点击:

How to pass a value from one Activity to another in Android?

android

提问by Santosh V M

I have created an Activity with a AutuCompleteTextView[ACTV] and button. I enter some text in the ACTV then press the button. After I press the button I want the Activity to go to another Activity. In the second Activity I just want to display the text entered in ACTV(of the first actvity) as a TextView.

我创建了一个带有 AutoCompleteTextView[ACTV] 和按钮的活动。我在 ACTV 中输入一些文本,然后按下按钮。按下按钮后,我希望活动转到另一个活动。在第二个活动中,我只想将在 ACTV(第一个活动)中输入的文本显示为 TextView。

I know how to start the second activity which is as below:

我知道如何开始第二个活动,如下所示:

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

I have coded this to obtain the text entered from the ACTV.

我对此进行了编码以获取从 ACTV 输入的文本。

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
CharSequence getrec=textView.getText();

My question here is how to pass "getrec" (after I press the button) from the first Activity to the second. And later recieve "getrec" in the second activity.

我的问题是如何将“getrec”(在我按下按钮后)从第一个活动传递到第二个活动。然后在第二个活动中收到“getrec”。

Please assume that I have created the event handler class for the button by using "onClick(View v)"

请假设我已经使用“onClick(View v)”为按钮创建了事件处理程序类

回答by DeRagan

You can use Bundle to do the same in Android

您可以使用 Bundle 在 Android 中执行相同的操作

Create the intent:

创建意图:

Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();

//Create the bundle
Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“stuff”, getrec);

//Add the bundle to the intent
i.putExtras(bundle);

//Fire that second activity
startActivity(i);

Now in your second activity retrieve your data from the bundle:

现在在您的第二个活动中从包中检索您的数据:

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String stuff = bundle.getString(“stuff”); 

回答by user1381420RKS

Standard way of passing data from one activity to another:

将数据从一个活动传递到另一个活动的标准方法:

If you want to send large number of data from one activity to another activity then you can put data in a bundle and then pass it using putExtra()method.

如果您想将大量数据从一个活动发送到另一个活动,那么您可以将数据放入一个包中,然后使用putExtra()方法传递它。

//Create the `intent`
 Intent i = new Intent(this, ActivityTwo.class);
String one="xxxxxxxxxxxxxxx";
String two="xxxxxxxxxxxxxxxxxxxxx";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“ONE”, one);
bundle.putString(“TWO”, two);  
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);

otherwise you can use putExtra()directly with intent to send data and getExtra()to get data.

否则,您可以putExtra()直接使用意图发送数据和getExtra()获取数据。

Intent i=new Intent(this, ActivityTwo.class);
i.putExtra("One",one);
i.putExtra("Two",two);
startActivity(i);

回答by Konstantin Burov

Thats trivial, use Intent.putExtra to pass data to activity you start. Use then Bundle.getExtra to retrieve it.

这很简单,使用 Intent.putExtra 将数据传递给您启动的活动。然后使用 Bundle.getExtra 来检索它。

There are lots of such questions already https://stackoverflow.com/search?q=How+to+pass+a+value+from+one+Activity+to+another+in+Androidbe sure to use search first next time.

已经有很多这样的问题了https://stackoverflow.com/search?q=How+to+pass+a+value+from+one+Activity+to+another+in+Android下次一定要先用search .

回答by Rishabh.G

Implement in this way

以这种方式实施

String i="hi";
Intent i = new Intent(this, ActivityTwo.class);
//Create the bundle
Bundle b = new Bundle();
//Add your data to bundle
b.putString(“stuff”, i);
i.putExtras(b);
startActivity(i);

Begin that second activity, inside this classto utilize the Bundle values use this code

开始第二个activity,在这里面class利用 Bundle 值使用这个代码

Bundle bundle = getIntent().getExtras();
String text= bundle.getString("stuff");

回答by Rohit Singh

Its simple If you are passing String X from A to B.
A--> B

很简单,如果您将字符串 X 从 A 传递到 B。A
--> B

In Activity A
1) Create Intent
2) Put data in intent using putExtra method of intent
3) Start activity

在活动 A 中
1) 创建意图
2) 使用意图的 putExtra 方法将数据放入意图
3) 开始活动

Intent i = new Intent(A.this, B.class);
i.putExtra("MY_kEY",X);

In Activity B
inside onCreate method
1) Get intent object
2) Get stored value using key(MY_KEY)

在 Activity B 中的
onCreate 方法
1) 获取意图对象
2) 使用 key(MY_KEY) 获取存储值

Intent intent = getIntent();
String result = intent.getStringExtra("MY_KEY");

This is the standard way to send data from A to B. you can send any data type, it could be int, boolean, ArrayList, String[]. Based on the datatype you stored in Activity as key, value pair retrieving method might differ like if you are passing int value then you will call

这是从 A 向 B 发送数据的标准方式。您可以发送任何数据类型,可以是 int、boolean、ArrayList、String[]。根据您作为键存储在 Activity 中的数据类型,值对检索方法可能会有所不同,例如如果您传递的是 int 值,那么您将调用

intent.getIntExtra("KEY");

You can even send Class objects too but for that, you have to make your class object implement the Serializable or Parceable interface.

您甚至可以发送 Class 对象,但为此,您必须使您的类对象实现 Serializable 或 Parceable 接口。

TransactionTooLargeException

交易过大异常

How much data you can send across size. If data exceeds a certain amount in size then you might get TransactionTooLargeException. Suppose you are trying to send bitmap across the activity and if the size exceeds certain data size then you might see this exception.

您可以跨大小发送多少数据。如果数据大小超过一定数量,那么您可能会收到 TransactionTooLargeException。假设您正在尝试跨活动发送位图,并且如果大小超过特定数据大小,那么您可能会看到此异常。

回答by Samie Naseem

in the first Activity:

在第一个活动中:

Intent i=new Intent(getApplicationContext,secondActivity.class);

i.putExtra("key",value);

startActivity(i);

and in the SecondActivity:

在第二个活动中:

String value=getIntent.getStringExtra("Key");

回答by Eddy

Sample Kotlin code as below:-

示例 Kotlin 代码如下:-

Page 1

第 1 页

val i = Intent(this, Page2::class.java)
            val getrec = list[position].promotion_id
            val bundle = Bundle()
            bundle.putString("stuff", getrec)
            i.putExtras(bundle)
            startActivity(i)

Page 2

第2页

        var bundle = getIntent().getExtras()
        var stuff = bundle.getString("stuff")