Mada za sehemu hiiWorking with data and multimediaMada 2
- Using local data storage
- Showing images and videos
Using Local Data Storage
Local data storage allows a mobile application to save and retrieve data directly on the device without needing an internet connection. This enables apps to remember user preferences, save work in progress, and store information like notes, contacts, or settings even after the app is closed and reopened.
When you use a mobile app and close it, you expect your data to be there the next time you open it. Local storage makes this possible by saving data to the device's memory. Without local storage, every time you opened an app it would be empty, and you would lose everything you entered.
1. SharedPreferences
SharedPreferences stores small amounts of data in key-value pairs. It is best for simple settings and user preferences.
- Stores: simple data like username, theme preference, sound on/off
- Data type: strings, numbers, booleans
- Example use: remembering whether the user wants dark mode or light mode
2. SQLite Database
SQLite is a lightweight relational database that stores structured data in tables. It is best for large amounts of related data.
- Stores: multiple records with multiple fields, like a list of contacts or transaction records
- Data type: structured data with rows and columns
- Example use: storing a list of daily expenses in a budgeting app
3. File Storage
File storage saves data as files (text, JSON, images) in the device's file system. It is best for storing complex data or media files.
- Stores: images, documents, JSON data
- Data type: any file format
- Example use: saving a profile photo or exporting data as a text file
| Data Size | Data Type | Recommended Storage |
|---|---|---|
| Very small (few settings) | Simple key-value | SharedPreferences |
| Large and structured | Tables with rows/columns | SQLite |
| Complex files | Images, documents | File Storage |
Consider a simple notes app where the user writes a note and expects it to be there when they return.
Step 1: Saving a Note
# Using SharedPreferences to save a note
note_content = "Buy 5kg of rice from the market"
# Save to SharedPreferences
preferences = getSharedPreferences("MyNotes", MODE_PRIVATE)
editor = preferences.edit()
editor.putString("saved_note", note_content)
editor.apply()
Step 2: Loading the Note
# Retrieve the saved note when app opens
preferences = getSharedPreferences("MyNotes", MODE_PRIVATE)
loaded_note = preferences.getString("saved_note", "")
# Display the note
print("Your saved note: " + loaded_note)
In this example:
"MyNotes"is the name of the storage file"saved_note"is the key we use to find our datagetString()returns the stored note, or an empty string""if nothing was saved
- SharedPreferences stores simple settings using keys and values
- SQLite stores structured data in tables for larger datasets
- File Storage stores images and complex files
- Choose storage based on how much data you have and how it is structured
Local storage is essential for building apps that feel personal and reliable, remembering user choices and protecting important data even when the device is offline.
In Tanzania, a small shop owner at a local market can use a simple inventory app to record items like sugar, cooking oil, and soap. By using local storage (SQLite), the app saves each sale and current stock directly on the phone, so the owner can check remaining stock even without internet access — useful in areas with poor network connectivity like many rural markets in Arusha or Mwanza.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Save, retrieve, and update data using local storage techniques
- Persist data between app sessions.
Tathmini ya matokeo
A functional app that efficiently uses local storage to handle data persistence.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how
- various local storage options and when to use each.
Principles: The student should explain the principles of:
- data integrity and security in local storage.
Theories: The student should explain:
- various local storage options and their appropriateness
- data serialization, storage limits, and file organization.
Circumstantial knowledge:
Detailed knowledge about:
- issues like limited storage space and data conflicts.
Swali
Which type of data is best suited for SharedPreferences storage?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado