Android应用程序中的捆绑包是什么?什么时候使用?


Bundle用于在活动之间传递数据。你可以创建一个bundle,将它传递给Intent来启动活动,然后可以从目标活动中使用。

bundle可以用来通过intent将任意数据从一个活动发送到另一个活动。当你广播一个Intent时,感兴趣的activity(和其他broadcastreceivers)会得到通知。一个intent可以包含一个Bundle,这样你就可以发送额外的数据。

bundle是键-值映射,因此在某种程度上它们类似于Hash,但它们并不严格限制于单个String / Foo对象映射。请注意,只有特定的数据类型被认为是“可打包的”,并且它们在Bundle API中被显式地拼写出来。

Update: When it comes to Android, there are two completely unrelated meanings to the term "bundle". One is detailed in my original answer below. The other is an app bundle. This is a newer archive file format (ending in .aap) that contains an Android app plus some additional metadata. You can upload an app bundle file instead of an application APK file to distribute your app through Google Play. App bundles have certain advantages over .apk files, but may not be compatible with other app stores (such as the Amazon App Store). These advantages are described in the documentation link included in my original answer.

最初的回答:

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

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

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

捆绑包通常用于在各种Android活动之间传递数据。这取决于你想传递什么类型的值,但是包可以保存所有类型的值并将它们传递给新活动。

你可以这样使用它:

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

你可以通过这样做来获取传递的值:

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

你可以在这里找到更多信息:

android-using-bundle-for-sharing-variables和 Passing-Bundles-Around-Activities

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

当屏幕旋转时,或者当另一个活动启动时,方法protected void onSaveInstanceState(Bundle outState)被调用,并且活动被销毁。稍后,创建该活动的另一个实例,并调用公共void onCreate(Bundle savedInstanceState)。当activity的第一个实例被创建时,bundle是空的;如果bundle不为空,活动将继续由它的前任开始的一些业务。

Android会自动将文本保存在文本框中,但它不会保存所有内容,有时会出现一些微妙的错误。

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

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

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

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


首先创建一个Bundle对象

Bundle b = new Bundle();

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

b.putString("myname", anystring);

现在,创建一个Intent对象

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

将bundle对象b传递给intent

in.putExtras(b);

开始第二项活动

startActivity(in);

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

Intent in = getIntent();

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

Bundle b = in.getExtras();

最后,获取与名为"myname"的键相关的字符串数据的值

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

Bundle用于在活动之间共享数据, 在oncreate()方法中保存应用程序的状态,这样应用程序就会知道它在哪里停止了… 我希望它能帮助你:)

只需要创建一个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);

在"this_is_the_next_class.class"

您可以像这样检索项目。

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);

使用bundle在intent对象的帮助下将数据从一个活动发送到另一个活动; 捆绑包保存可以是任何类型的数据。

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

第一步:第一个活动

Bundle b=new Bundle();

b.putString("mkv",anystring);

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

in.putExtras(b);

startActivity(in);

第二步:第二项活动

Intent in=getIntent();

Bundle b=in.getExtras();

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

我认为这对你很有用...........

Bundle不仅用于在两个不同的组件之间传输数据,更重要的是用于恢复在activity被销毁为新activity之前存储的值。

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

包: 从String值到各种Parcelable类型的映射。

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

当我们调用onPause(),然后调用onStop(),然后以相反的顺序调用onStop()到onPause()。

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

第一个活动:

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());

第二个活动:

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