Junior — Middle
Can you explain what a Bundle object is in the context of development?
sobes.tech AI
Answer from AI
In Android, the Bundle object is a container for storing key-value pairs, used for passing data between application components, such as between an Activity or Fragment. It allows saving and restoring state, as well as passing primitive types, strings, arrays, and objects that implement the Parcelable interface.
Example of using Bundle to pass data between Activity:
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("key", "value");
intent.putExtras(bundle);
startActivity(intent);
In SecondActivity, you can retrieve the data as follows:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String value = bundle.getString("key");
}