Sobes.tech
Junior — Middle

How can you extract information from a Bundle object in Android?

sobes.tech AI

Answer from AI

In Android, the Bundle object is used to pass data between components (for example, between an Activity or Fragment). To extract information from a Bundle, you need to use data retrieval methods corresponding to the value type.

Example of extracting data from a Bundle:

Bundle bundle = getIntent().getExtras(); // Get the Bundle from Intent
if (bundle != null) {
    String name = bundle.getString("name");
    int age = bundle.getInt("age");
    boolean isActive = bundle.getBoolean("isActive");
    // Use the retrieved data
}

Main methods for extraction:

  • getString(String key) — for strings
  • getInt(String key) — for integers
  • getBoolean(String key) — for boolean values
  • getSerializable(String key) — for serializable objects

It is important to check that the Bundle is not null to avoid NullPointerException.