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


当前回答

第一个活动:

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

其他回答

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

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

第一个活动:

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

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

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

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

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

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

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