Android应用程序中的捆绑包是什么?什么时候使用?
当前回答
Bundle不仅用于在两个不同的组件之间传输数据,更重要的是用于恢复在activity被销毁为新activity之前存储的值。
例如EditText小部件中的文本或ListView的滚动位置。
其他回答
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框架依赖于这个属性。
我建议您阅读有关应用程序基础的文档。这解释了什么是捆绑包和意图,以及它们的用途。
包: 从String值到各种Parcelable类型的映射。
Bundle通常用于在android的各个活动之间传递数据。
当我们调用onPause(),然后调用onStop(),然后以相反的顺序调用onStop()到onPause()。
系统用来恢复先前状态的保存数据称为“实例状态”,是存储在Bundle对象中的键-值对的集合。
使用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用于在活动之间传递数据。你可以创建一个bundle,将它传递给Intent来启动活动,然后可以从目标活动中使用。
第一个活动:
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");
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。