Android 在 startActivity() 上传递 Bundle?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/768969/
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
Passing a Bundle on startActivity()?
提问by yanchenko
What's the correct way to pass a bundle to the activity that is being launched from the current one? Shared properties?
将捆绑包传递给从当前活动启动的活动的正确方法是什么?共享属性?
回答by Jeremy Logan
You have a few options:
您有几个选择:
1) Use the Bundlefrom the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) Create a new Bundle
2) 创建一个新的 Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
3) Use the putExtra()shortcut method of the Intent
3)使用Intent的putExtra()快捷方法
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
Then, in the launched Activity, you would read them via:
然后,在启动的 Activity 中,您可以通过以下方式阅读它们:
String value = getIntent().getExtras().getString(key)
NOTE:Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.
注意:Bundle 对所有原始类型、Parcelables 和 Serializable 都有“get”和“put”方法。我只是将字符串用于演示目的。
回答by DustinB
You can use the Bundle from the Intent:
您可以使用 Intent 中的 Bundle:
Bundle extras = myIntent.getExtras();
extras.put*(info);
Or an entire bundle:
或整个捆绑包:
myIntent.putExtras(myBundle);
Is this what you're looking for?
这是你要找的吗?
回答by IntelliJ Amiya
Passing data from one Activity to Activity in android
将数据从一个活动传递到android中的活动
An intent contains the action and optionally additional data. The data can be passed to other activity using intent putExtra()
method. Data is passed as extras and are key/value pairs
. The key is always a String. As value you can use the primitive data types int, float, chars, etc. We can also pass Parceable and Serializable
objects from one activity to other.
意图包含操作和可选的附加数据。可以使用意图putExtra()
方法将数据传递给其他活动。数据作为附加项传递,并且是key/value pairs
. 键始终是一个字符串。作为值,您可以使用原始数据类型 int、float、chars 等。我们还可以将Parceable and Serializable
对象从一个活动传递到另一个活动。
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);
Retrieving bundle data from android activity
从 android 活动中检索包数据
You can retrieve the information using getData()
methods on the Intent object. The Intentobject can be retrieved via the getIntent()
method.
您可以使用getData()
Intent 对象上的方法检索信息 。的意图对象可以通过被检索getIntent()
方法。
Intent intent = getIntent();
if (null != intent) { //Null Checking
String StrData= intent.getStringExtra(KEY);
int NoOfData = intent.getIntExtra(KEY, defaultValue);
boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
char charData = intent.getCharExtra(KEY, defaultValue);
}
回答by Neha Mehta
You can pass values from one activity to another activity using the Bundle. In your current activity, create a bundle and set the bundle for the particular value and pass that bundle to the intent.
您可以使用 Bundle 将值从一个活动传递到另一个活动。在您当前的活动中,创建一个包并为特定值设置包并将该包传递给意图。
Intent intent = new Intent(this,NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString(key,value);
intent.putExtras(bundle);
startActivity(intent);
Now in your NewActivity, you can get this bundle and retrive your value.
现在在您的 NewActivity 中,您可以获取此包并检索您的值。
Bundle bundle = getArguments();
String value = bundle.getString(key);
You can also pass data through the intent. In your current activity, set intent like this,
您还可以通过意图传递数据。在您当前的活动中,像这样设置意图,
Intent intent = new Intent(this,NewActivity.class);
intent.putExtra(key,value);
startActivity(intent);
Now in your NewActivity, you can get that value from intent like this,
现在在您的 NewActivity 中,您可以从这样的意图中获取该值,
String value = getIntent().getExtras().getString(key);
回答by trustidkid
Write this is the activity you are in:
写下这是您参与的活动:
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtras("string_name","string_to_pass");
startActivity(intent);
In the NextActivity.java
在 NextActivity.java 中
Intent getIntent = getIntent();
//call a TextView object to set the string to
TextView text = (TextView)findViewById(R.id.textview_id);
text.setText(getIntent.getStringExtra("string_name"));
This works for me, you can try it.
这对我有用,你可以试试。
来源:https: //www.c-sharpcorner.com/article/how-to-send-the-data-one-activity-to-another-activity-in-android-application/
回答by Pouria Hemati
you can use this code in your first activity:
您可以在您的第一个活动中使用此代码:
Intent i = new Intent(Context, your second activity.class);
i.putExtra("key_value", "your object");
startActivity(i);
and get object in second activity:
并在第二个活动中获取对象:
Intent in = getIntent();
Bundle content = in.getExtras();
// check null
if (content != null) {
String content = content_search.getString("key_value");
}