Android应用程序中的“捆绑”是什么

时间:2021-04-04 23:06:09

What is a bundle in an Android application? When to use it?

什么是Android应用程序包?什么时候使用它呢?

12 个解决方案

#1


237  

Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

bundle通常用于在各种Android活动之间传递数据。它取决于您希望传递的值的类型,但是bundle可以保存所有类型的值并将它们传递给新的活动。

You can use it like this:

你可以这样使用:

Intent intent = new...
Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("myKey", AnyValue);  
startActivity(intent);

You can get the passed values by doing:

您可以通过以下方式获得传递的值:

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

You can find more info at:

你可在以下网页找到更多资料:

#2


61  

I have to add that bundles are used by activities to pass data to themselves in the future.

我必须补充的是,在将来,活动使用bundle将数据传递给它们自己。

When the screen rotates, or when another activity is started, the method protected void onSaveInstanceState(Bundle outState) is invoked, and the activity is destroyed. Later, another instance of the activity is created, and public void onCreate(Bundle savedInstanceState) is called. When the first instance of activity is created, the bundle is null; and if the bundle is not null, the activity continues some business started by its predecessor.

当屏幕旋转或启动另一个活动时,将调用保护void onSaveInstanceState(Bundle outState)的方法,并销毁该活动。稍后,将创建活动的另一个实例,并调用public void onCreate(Bundle savedInstanceState)。创建活动的第一个实例时,该包为空;如果bundle不是null,则该活动将继续其前任启动的某些业务。

Android automatically saves the text in text fields, but it does not save everything, and subtle bugs sometimes appear.

Android会自动将文本保存在文本字段中,但它并不能保存所有内容,有时还会出现一些细微的bug。

The most common anti-pattern, though, is assuming that onCreate() does just initialization. It is wrong, because it also must restore the state.

不过,最常见的反模式是假设onCreate()只执行初始化。这是错误的,因为它也必须恢复国家。

There is an option to disable this "re-create activity on rotation" behavior, but it will not prevent restart-related bugs, it will just make them more difficult to mention.

有一个选项可以禁用这个“在旋转时重新创建活动”的行为,但是它不会阻止与重新启动相关的bug,它只会使它们更难提及。

Note also that the only method whose call is guaranteed when the activity is going to be destroyed is onPause(). (See the activity life cycle graph in the docs.)

还要注意,在活动将要被破坏时,唯一保证调用的方法是onPause()。(请参阅文档中的活动生命周期图。)

#3


55  

Pass data between activities by using Bundle and Intent objects.

通过使用Bundle和Intent对象在活动之间传递数据。


Your first create a Bundle object

首先创建一个Bundle对象

Bundle b = new Bundle();

Then, associate the string data stored in anystring with bundle key "myname"

然后,将存储在anystring中的字符串数据与bundle key“myname”关联起来

b.putString("myname", anystring);

Now, create an Intent object

现在,创建一个Intent对象。

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

Pass bundle object b to the intent

将bundle对象b传递给意图

in.putExtras(b);

and start second activity

并开始第二个活动

startActivity(in);

In the second activity, we have to access the data passed from the first activity

在第二个活动中,我们必须访问从第一个活动传递的数据

Intent in = getIntent();

Now, you need to get the data from the bundle

现在,您需要从包中获取数据

Bundle b = in.getExtras();

Finally, get the value of the string data associated with key named "myname"

最后,获取与名为“myname”的键关联的字符串数据的值

String s = b.getString("myname");

#4


28  

A Bundle is very much like a Java Map object that maps String keys to values. It's used to pass information between activities and other application components. It's also used by the framework to capture and restore state information.

Bundle非常类似于将字符串键映射到值的Java映射对象。它用于在活动和其他应用程序组件之间传递信息。框架还使用它来捕获和恢复状态信息。

The reason Android doesn't use plain old Map objects for this is that Map is too flexible; it can contain objects (such as, say, I/O streams) that cannot be serialized. The Bundle API restricts the types of objects that can be added to a bundle in such a way that the bundle's contents are guaranteed to be serializable. The Android framework relies on this property.

Android不使用普通的旧地图对象的原因是地图太灵活了;它可以包含不能序列化的对象(例如,比如I/O流)。Bundle API限制了可以添加到Bundle的对象的类型,以确保该Bundle的内容是可序列化的。Android框架依赖于此属性。

I suggest that you read the documentation on Application Fundamentals. This explains, among other things, what bundles and intents are and what they are used for.

我建议您阅读关于应用程序基础的文档。这解释了捆绑包和意图以及它们的用途。

#5


14  

Bundles can be used to send arbitrary data from one activity to another by way of Intents. When you broadcast an Intent, interested Activities (and other BroadcastRecievers) will be notified of this. An intent can contain a Bundle so that you can send extra data along with the Intent.

束可以用于通过意图将任意数据从一个活动发送到另一个活动。当你广播一个意图,感兴趣的活动(和其他广播)将被通知这。意图可以包含一个包,这样您就可以在意图的同时发送额外的数据。

Bundles are key-value mappings, so in a way they are like a Hash, but they are not strictly limited to a single String / Foo object mapping. Note that only certain data types are considered "Parcelable" and they are explicitly spelled out in the Bundle API.

bundle是键-值映射,因此在某种程度上它们就像散列,但它们并不严格限制于单个字符串/ Foo对象映射。注意,只有某些数据类型被认为是“可分割的”,它们在Bundle API中被显式地拼写出来。

#6


7  

Just create a bundle,

只是创建一个包,


Bundle simple_bundle=new Bundle();
simple_bundle.putString("item1","value1");
Intent i=new Intent(getApplicationContext(),this_is_the_next_class.class);
i.putExtras(simple_bundle);
startActivity(i);

IN the "this_is_the_next_class.class"

在“this_is_the_next_class.class”

You can retrieve the items like this.

您可以像这样检索项目。

Intent receive_i=getIntent();
Bundle my_bundle_received=receive_i.getExtras();
my_bundle_received.get("item1");
Log.d("Value","--"+my_bundle_received.get("item1").toString);

#7


5  

Bundle is used to pass data between Activities. You can create a bundle, pass it to Intent that starts the activity which then can be used from the destination activity.

Bundle用于在活动之间传递数据。您可以创建一个bundle,将其传递到意图,该意图启动活动,然后可以从目标活动中使用该活动。

#8


3  

Bundle:- A mapping from String values to various Parcelable types.

Bundle:-从字符串值到各种可分割类型的映射。

Bundle is generally used for passing data between various activities of android.

Bundle通常用于在android的各种活动之间传递数据。

when we call onPause() then onStop() and then in reverse order onStop() to onPause().

当我们调用onPause()然后调用onStop(),然后按反向顺序调用onStop()到onPause()。

The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

系统用来恢复先前状态的保存数据称为“实例状态”,是存储在Bundle对象中的键-值对的集合。

#9


2  

bundle is used to share data between activities , and to save state of app in oncreate() method so that app will come to know where it was stopped ... I hope it helps :)

bundle用于在活动之间共享数据,并在oncreate()方法中保存app状态,让app知道它在哪里停止……我希望它有帮助:)

#10


0  

use of bundle send data from one activity to another activity with the help of intent object; Bundle hold the data that can be any type.

使用bundle在intent对象的帮助下将数据从一个活动发送到另一个活动;Bundle包含任何类型的数据。

Now I tell that how to create bundle passing data between two activity.

现在我告诉您如何在两个活动之间创建bundle传递数据。

Step 1: On First activity

第一步:第一步

Bundle b=new Bundle();

b.putString("mkv",anystring);

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

in.putExtras(b);

startActivity(in);

Step 2: On Second Activity

第二步:第二次活动

Intent in=getIntent();

Bundle b=in.getExtras();

String s=b.getString("mkv");

I think this is useful for you...........

我认为这对你来说很有用。

#11


0  

Bundle is not only to transfer data between two different components but more importantly it is used to restore the values stored before activity is destroyed into new activity.

Bundle不仅用于在两个不同的组件之间传输数据,更重要的是,它用于将在活动被破坏之前存储的值恢复为新的活动。

such as the text in an EditText widget or the scroll position of a ListView.

例如EditText小部件中的文本或ListView的滚动位置。

#12


0  

First activity:

第一个活动:

String food = (String)((Spinner)findViewById(R.id.food)).getSelectedItem();
RadioButton rb = (RadioButton) findViewById(R.id.rb);
Intent i = new Intent(this,secondActivity.class);
i.putExtra("food",food);
i.putExtra("rb",rb.isChecked());

Second activity:

第二个活动:

String food = getIntent().getExtras().getString("food");
Boolean rb = getIntent().getExtras().getBoolean("rb");

#1


237  

Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

bundle通常用于在各种Android活动之间传递数据。它取决于您希望传递的值的类型,但是bundle可以保存所有类型的值并将它们传递给新的活动。

You can use it like this:

你可以这样使用:

Intent intent = new...
Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("myKey", AnyValue);  
startActivity(intent);

You can get the passed values by doing:

您可以通过以下方式获得传递的值:

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

You can find more info at:

你可在以下网页找到更多资料:

#2


61  

I have to add that bundles are used by activities to pass data to themselves in the future.

我必须补充的是,在将来,活动使用bundle将数据传递给它们自己。

When the screen rotates, or when another activity is started, the method protected void onSaveInstanceState(Bundle outState) is invoked, and the activity is destroyed. Later, another instance of the activity is created, and public void onCreate(Bundle savedInstanceState) is called. When the first instance of activity is created, the bundle is null; and if the bundle is not null, the activity continues some business started by its predecessor.

当屏幕旋转或启动另一个活动时,将调用保护void onSaveInstanceState(Bundle outState)的方法,并销毁该活动。稍后,将创建活动的另一个实例,并调用public void onCreate(Bundle savedInstanceState)。创建活动的第一个实例时,该包为空;如果bundle不是null,则该活动将继续其前任启动的某些业务。

Android automatically saves the text in text fields, but it does not save everything, and subtle bugs sometimes appear.

Android会自动将文本保存在文本字段中,但它并不能保存所有内容,有时还会出现一些细微的bug。

The most common anti-pattern, though, is assuming that onCreate() does just initialization. It is wrong, because it also must restore the state.

不过,最常见的反模式是假设onCreate()只执行初始化。这是错误的,因为它也必须恢复国家。

There is an option to disable this "re-create activity on rotation" behavior, but it will not prevent restart-related bugs, it will just make them more difficult to mention.

有一个选项可以禁用这个“在旋转时重新创建活动”的行为,但是它不会阻止与重新启动相关的bug,它只会使它们更难提及。

Note also that the only method whose call is guaranteed when the activity is going to be destroyed is onPause(). (See the activity life cycle graph in the docs.)

还要注意,在活动将要被破坏时,唯一保证调用的方法是onPause()。(请参阅文档中的活动生命周期图。)

#3


55  

Pass data between activities by using Bundle and Intent objects.

通过使用Bundle和Intent对象在活动之间传递数据。


Your first create a Bundle object

首先创建一个Bundle对象

Bundle b = new Bundle();

Then, associate the string data stored in anystring with bundle key "myname"

然后,将存储在anystring中的字符串数据与bundle key“myname”关联起来

b.putString("myname", anystring);

Now, create an Intent object

现在,创建一个Intent对象。

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

Pass bundle object b to the intent

将bundle对象b传递给意图

in.putExtras(b);

and start second activity

并开始第二个活动

startActivity(in);

In the second activity, we have to access the data passed from the first activity

在第二个活动中,我们必须访问从第一个活动传递的数据

Intent in = getIntent();

Now, you need to get the data from the bundle

现在,您需要从包中获取数据

Bundle b = in.getExtras();

Finally, get the value of the string data associated with key named "myname"

最后,获取与名为“myname”的键关联的字符串数据的值

String s = b.getString("myname");

#4


28  

A Bundle is very much like a Java Map object that maps String keys to values. It's used to pass information between activities and other application components. It's also used by the framework to capture and restore state information.

Bundle非常类似于将字符串键映射到值的Java映射对象。它用于在活动和其他应用程序组件之间传递信息。框架还使用它来捕获和恢复状态信息。

The reason Android doesn't use plain old Map objects for this is that Map is too flexible; it can contain objects (such as, say, I/O streams) that cannot be serialized. The Bundle API restricts the types of objects that can be added to a bundle in such a way that the bundle's contents are guaranteed to be serializable. The Android framework relies on this property.

Android不使用普通的旧地图对象的原因是地图太灵活了;它可以包含不能序列化的对象(例如,比如I/O流)。Bundle API限制了可以添加到Bundle的对象的类型,以确保该Bundle的内容是可序列化的。Android框架依赖于此属性。

I suggest that you read the documentation on Application Fundamentals. This explains, among other things, what bundles and intents are and what they are used for.

我建议您阅读关于应用程序基础的文档。这解释了捆绑包和意图以及它们的用途。

#5


14  

Bundles can be used to send arbitrary data from one activity to another by way of Intents. When you broadcast an Intent, interested Activities (and other BroadcastRecievers) will be notified of this. An intent can contain a Bundle so that you can send extra data along with the Intent.

束可以用于通过意图将任意数据从一个活动发送到另一个活动。当你广播一个意图,感兴趣的活动(和其他广播)将被通知这。意图可以包含一个包,这样您就可以在意图的同时发送额外的数据。

Bundles are key-value mappings, so in a way they are like a Hash, but they are not strictly limited to a single String / Foo object mapping. Note that only certain data types are considered "Parcelable" and they are explicitly spelled out in the Bundle API.

bundle是键-值映射,因此在某种程度上它们就像散列,但它们并不严格限制于单个字符串/ Foo对象映射。注意,只有某些数据类型被认为是“可分割的”,它们在Bundle API中被显式地拼写出来。

#6


7  

Just create a bundle,

只是创建一个包,


Bundle simple_bundle=new Bundle();
simple_bundle.putString("item1","value1");
Intent i=new Intent(getApplicationContext(),this_is_the_next_class.class);
i.putExtras(simple_bundle);
startActivity(i);

IN the "this_is_the_next_class.class"

在“this_is_the_next_class.class”

You can retrieve the items like this.

您可以像这样检索项目。

Intent receive_i=getIntent();
Bundle my_bundle_received=receive_i.getExtras();
my_bundle_received.get("item1");
Log.d("Value","--"+my_bundle_received.get("item1").toString);

#7


5  

Bundle is used to pass data between Activities. You can create a bundle, pass it to Intent that starts the activity which then can be used from the destination activity.

Bundle用于在活动之间传递数据。您可以创建一个bundle,将其传递到意图,该意图启动活动,然后可以从目标活动中使用该活动。

#8


3  

Bundle:- A mapping from String values to various Parcelable types.

Bundle:-从字符串值到各种可分割类型的映射。

Bundle is generally used for passing data between various activities of android.

Bundle通常用于在android的各种活动之间传递数据。

when we call onPause() then onStop() and then in reverse order onStop() to onPause().

当我们调用onPause()然后调用onStop(),然后按反向顺序调用onStop()到onPause()。

The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

系统用来恢复先前状态的保存数据称为“实例状态”,是存储在Bundle对象中的键-值对的集合。

#9


2  

bundle is used to share data between activities , and to save state of app in oncreate() method so that app will come to know where it was stopped ... I hope it helps :)

bundle用于在活动之间共享数据,并在oncreate()方法中保存app状态,让app知道它在哪里停止……我希望它有帮助:)

#10


0  

use of bundle send data from one activity to another activity with the help of intent object; Bundle hold the data that can be any type.

使用bundle在intent对象的帮助下将数据从一个活动发送到另一个活动;Bundle包含任何类型的数据。

Now I tell that how to create bundle passing data between two activity.

现在我告诉您如何在两个活动之间创建bundle传递数据。

Step 1: On First activity

第一步:第一步

Bundle b=new Bundle();

b.putString("mkv",anystring);

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

in.putExtras(b);

startActivity(in);

Step 2: On Second Activity

第二步:第二次活动

Intent in=getIntent();

Bundle b=in.getExtras();

String s=b.getString("mkv");

I think this is useful for you...........

我认为这对你来说很有用。

#11


0  

Bundle is not only to transfer data between two different components but more importantly it is used to restore the values stored before activity is destroyed into new activity.

Bundle不仅用于在两个不同的组件之间传输数据,更重要的是,它用于将在活动被破坏之前存储的值恢复为新的活动。

such as the text in an EditText widget or the scroll position of a ListView.

例如EditText小部件中的文本或ListView的滚动位置。

#12


0  

First activity:

第一个活动:

String food = (String)((Spinner)findViewById(R.id.food)).getSelectedItem();
RadioButton rb = (RadioButton) findViewById(R.id.rb);
Intent i = new Intent(this,secondActivity.class);
i.putExtra("food",food);
i.putExtra("rb",rb.isChecked());

Second activity:

第二个活动:

String food = getIntent().getExtras().getString("food");
Boolean rb = getIntent().getExtras().getBoolean("rb");