what is bundle in android
It is known that Intents are used in Android to pass to the data from one activity to another. But there is one another way, that can be used to pass the data from one activity to another in a better way and less code space ie by using Bundles in Android. Android Bundles are generally used for passing data from one activity to another. Basically here concept of key-value pair is used where the data that one wants to pass is the value of the map, which can be later retrieved by using the key. Bundles are used with intent and values are sent and retrieved in the same fashion, as it is done in the case of Intent. It depends on the user what type of values the user wants to pass, but bundles can hold all types of values (int, String, boolean, char) and pass them to the new activity.
What is the use of bundles in Android?
Bundle is a class in android which is used to pass data from one activity to another activity within an android application. We can pass data using key and value pairs using bundles. We can pass the data using the key and can use that same key to retrive the data which is passed for that key.
Intent intent = new Intent(this, SecondActivity.class);
// creating a bundle object
Bundle bundle = new Bundle();
// storing the string value in the bundle
// which is mapped to key
bundle.putString("key1", "GFG :- Main Activity");
// passing the bundle into the intent
intent.putExtras(bundle);
// starting the intent
startActivity(intent);
No comments:
Post a Comment