How Android Intent putExtra() work? Under the hood …
Today we will discuss some basic points about intent putExtra(), How internally it works, and which data structure is used for it?
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("k","v")
startActivity(intent)
- When we write intent.putExtra(“k”,”v”) and click on this function
public @NonNull Intent putExtra(String name, @Nullable String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
we see that it creates a Bundle object and adds it into mExtras.putString(name, value); and mExtras which is a bundle object, defines the global of the Intent class
Bundle mExtras;
after this we see mExtras.putString(name, value); is used to store name values,
when we click on mExtras.putString(name, value);
public void putString(@Nullable String key, @Nullable String value) {
unparcel();
mMap.put(key, value);
}
here we observe mMap.put(key, value); and mMap is ArrayMap<K,V> data structure that is designed to be more memory efficient than the traditional HashMap<K,V>
ArrayMap<String, Object> mMap = null;
Points To Remember
- The Bundle object is created when putExtra() is used.
- Internally ArrayMap<K,V> data structure is used to store data.
Kindly clapping …
Feel free to follow me on LinkedIn
Thanks for reading, and I'll come again with new learnings soon