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");
我认为这对你很有用...........
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框架依赖于这个属性。
我建议您阅读有关应用程序基础的文档。这解释了什么是捆绑包和意图,以及它们的用途。
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用于在活动之间传递数据。你可以创建一个bundle,将它传递给Intent来启动活动,然后可以从目标活动中使用。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件