The successful organisation of local data is critical for mobile applications, notably for offline capabilities. Android has solutions such as SQLite and Room to implement. Room is a part of Jetpack, it provides common database operations by means of annotations, compile-time checks, which gives a number of notable benefits against other SQLite interfaces.
In the context of Android applications, there is no question that local storage remains helpful for storing users’ information and preferences. Android offers two main local storage options:
This blog will explain how both are utilized and when to employ, and each storage solution highlighted in this blog.
Room:
SharedPreferences:
1. What is SharedPreferences?
SharedPreferences is a simple, low cost data storage mechanism for storing key value pairs like user settings and session ID.
2. Example Code: Storing and Retrieving Data
Step 1: Storing Data
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get SharedPreferences instance
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
// Store data
editor.putString("username", "JohnDoe");
editor.putInt("userAge", 30);
editor.apply();
}
}
Step 2: Retrieving Data
// Retrieve data
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String username = sharedPreferences.getString("username", "DefaultName");
int userAge = sharedPreferences.getInt("userAge", 0);
System.out.println("Username: " + username + ", Age: " + userAge);
It means that you must always call either apply() or commit() if there have been changes. This means that keys, no matter in which part of the system they are going to be located, should be named consistently. For accessing data securely SharedPreferences can be encrypted using some libraries like EncryptedSharedPreferences.
Using Room Database
See the Room Database Example section of the earlier post to know how to get started with and use Room. Here is a quick comparison of the capabilities of the aforementioned two tools namely SQLite vs SharedPreferences.
Combining Room and SharedPreferences
You are going to find some cases where you will have to make use of both Room and SharedPreferences. For instance:
Example: Combining Both
For the user preference we use shared preferences and for the user details we use the room databases.
Best Practices for Local Data Management
Getting the right storage method is critical especially in the context of Android and determining this is done through a database selection process. Room is most suitable to work with structured relational data, and SharedPreferences is okay if you only need to store simple keys and values. When implemented together, the management of both can translate to a strong form of app architecture which transforms user experience as well as performance.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our Android Expertise.
0