2

I have encountered a case where the data is being read/written to/from SharedPreferences instead of using Intent to pass it across the Activities. Ignoring the use case argument for using Intents and SharedPreferences for a moment, I would like to know if Android writes an Intent to disk before sending it to the intended Activity.

Because, if it does, there would not be any performance difference between using an Intent and a SharedPreference and I might go with the existing flow with SharedPreference.

So, does it?

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • No, I think there is no way to write intent on the disk. You have to send the string data directly using `putExtra`. But if you want to pass the `Model class` to the intent you can use [Parcelable](https://developer.android.com/reference/android/os/Parcelable) – Sunny Aug 29 '18 at 05:46

2 Answers2

0

Intent is only in memory and absolutely better than SharedPreference, which is formatted to xml and then read/write to disk.

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
0

Where shared preferences are stored in disk? (source)

SharedPreferences are stored in an xml file in the app data folder, i.e.

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml

or the default preferences at:

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml

SharedPreferences added during runtime are not stored in the Eclipse project.

Note: Accessing /data/data/ requires superuser privileges

What about Intents? Wait, what is an Intent?

The intent is an "intention" to perform an action; in other words, a messaging object you can use to request an action from another app component. It can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

(additional info) In intents, the data to operate on, such as a personal record in the contacts database, expressed as a Uri.

If the above statement does not make an abstract visualization in your mind, simple answer would be

"Intent is just passing of data from one "Class"/"Object" to another and everything resides inside "The Memory" perhaps cache too.

Daksh Gargas
  • 3,498
  • 2
  • 23
  • 37