There are two ways to pass data between activities in Android:
Using intents: Intents are objects that are used to start activities, services, and broadcast receivers. They can also be used to pass data between activities. To pass data using intents, you need to use the
putExtra() method to add the data to the intent. The data can be of any type, such as strings, integers, or objects. When the activity is started, the data will be passed to the activity's
onCreate() method.
Using shared preferences: Shared preferences are a way to store small amounts of data in a file on the device. They can be used to store data that needs to be persisted across app sessions, such as user settings or login information. To pass data using shared preferences, you need to store the data in the shared preferences file in the first activity. Then, in the second activity, you can retrieve the data from the shared preferences file.
Here is an example of how to pass data using intents:
Code snippet
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("name", "John Doe");
startActivity(intent);
In this example, we are creating an intent to start the SecondActivity class. We are also adding a string with the key "name" and the value "John Doe" to the intent. When the
SecondActivity is started, the data will be passed to the activity's
onCreate() method.
Here is an example of how to pass data using shared preferences:
Code snippet
SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
String name = sharedPreferences.getString("name", "");
In this example, we are getting the data from the shared preferences file. The file is named "my_preferences" and it is stored in the private mode. The key of the data is "name" and the default value is an empty string.
Which method you use to pass data between activities will depend on your specific needs. If you need to pass a small amount of data, then using intents is a good option. If you need to pass a larger amount of data or if you need to persist the data across app sessions, then using shared preferences is a good option.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are two ways to pass data between activities in Android:
putExtra()method to add the data to the intent. The data can be of any type, such as strings, integers, or objects. When the activity is started, the data will be passed to the activity'sonCreate()method.Here is an example of how to pass data using intents:
Code snippet
In this example, we are creating an intent to start the
SecondActivityclass. We are also adding a string with the key "name" and the value "John Doe" to the intent. When theSecondActivityis started, the data will be passed to the activity'sonCreate()method.Here is an example of how to pass data using shared preferences:
Code snippet
In this example, we are getting the data from the shared preferences file. The file is named "my_preferences" and it is stored in the private mode. The key of the data is "name" and the default value is an empty string.
Which method you use to pass data between activities will depend on your specific needs. If you need to pass a small amount of data, then using intents is a good option. If you need to pass a larger amount of data or if you need to persist the data across app sessions, then using shared preferences is a good option.