Sobes.tech
Junior — Middle

Is it possible to pass any data types using the putExtra method in Intent?

sobes.tech AI

Answer from AI

The putExtra method in Intent allows passing data of various types, but not all. It supports primitive types (int, boolean, float, etc.), strings, arrays, as well as objects that implement the Parcelable or Serializable interfaces. If you try to pass an object that does not implement these interfaces, the application will throw an error at runtime. Therefore, for passing complex objects, they need to be either serialized or made Parcelable.

Example of passing a string and a Parcelable object:

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("username", "user123");
intent.putExtra("userData", userParcelableObject); // userParcelableObject implements Parcelable
startActivity(intent);