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


当前回答

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


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

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