Sure, here is an example of how to use an Intent and a Bundle to transfer data from one activity to another:
Code snippet
// Create an Intent to start the second activity.
Intent intent = new Intent(this, SecondActivity.class);
// Add some data to the Intent.
Bundle bundle = new Bundle();
bundle.putString("data", "This is the data");
intent.putExtras(bundle);
// Start the activity.
startActivity(intent);
In the second activity, you can retrieve the data that was passed in the Intent using the
getExtras() method.
Code snippet
// Get the data from the Intent.
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("data");
The data variable will now contain the string that was passed in the Intent.
Here is an example of how to use a Bundle to store a more complex data structure:
Code snippet
// Create a Bundle to store the data.
Bundle bundle = new Bundle();
// Add a person object to the Bundle.
Person person = new Person();
person.setName("John Doe");
person.setAge(30);
bundle.putSerializable("person", person);
// Start the activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
In the second activity, you can retrieve the person object from the Bundle using the
getSerializable() method.
Code snippet
// Get the person object from the Bundle.
Bundle bundle = getIntent().getExtras();
Person person = (Person) bundle.getSerializable("person");
The person variable will now contain the person object that was passed in the Bundle.
Aryan Kumar
27-Jun-2023Sure, here is an example of how to use an Intent and a Bundle to transfer data from one activity to another:
Code snippet
In the second activity, you can retrieve the data that was passed in the Intent using the
getExtras()
method.Code snippet
The
data
variable will now contain the string that was passed in the Intent.Here is an example of how to use a Bundle to store a more complex data structure:
Code snippet
In the second activity, you can retrieve the person object from the Bundle using the
getSerializable()
method.Code snippet
The
person
variable will now contain the person object that was passed in the Bundle.Anonymous User
18-Jan-2016