android offline handling innovationm blog

Android Offline Handling

Android offline handling basically represents the local storage data handling when there is no internet connection to get the basic required information. If not handled, the screen will be out of data and result in a blank screen which ultimately affects the UI experience.

There are two ways to handle local data :

  1. NoSql 
  2. SQL


Storing data in NoSql format or JSON format is one of the approaches to handle data offline. PaperDB and better use of SharedPreferences are the two ways to store data in JSON format. Since SharedPreferences only stores the primitive data types, we can convert the class object into JSON string and store them.


val sharedPreferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("key", "value")
editor.putInt("key", 0)
editor.putBoolean("key", true)
editor.putFloat("key", 0.0f)

# Storing class object into shared preferences


val user = User(
   name = etName.text.toString(),
   address = etAddress.text.toString(),
   phoneNo = etPhoneNumber.text.toString()
val userJson = Gson().toJson(user, User ::
editor.putString("user", userJson  ")

# Fetch Data from SharedPreference


sharedPreferences.getBoolean("key", false)
sharedPreferences.getFloat("key", 0.0f)
sharedPreferences.getString("key", null)
sharedPreferences.getInt("key", 0)
val userJson = sharedPreferences.getString("user", null)
val user = Gson().fromJson<User>(userJson ,



Paper’s aim is to provide a simple yet fast object storage option for Android. It allows us to use Java/Kotlin classes as is: without annotations, factory methods, mandatory class extensions etc. Moreover adding or removing fields to data classes is no longer a pain – all data structure changes are handled automatically.



  1. add dependency in build.gradle(app) file implementation
  2. In Application class, add Paper.init(context) inside onCreate method


   .write(“user”, user) // store and update data havingkey user’“user”) // delete data having key user“user”) // checks whether user key is available in database

val user =

   .read<User>(“user”) // fetch data having key user


The other approach to handle offline data to store data in table structured row and column. Data is manipulated with the help of queries.


SQLite is an open source SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features.

For more learnings go to

Room Database

It is a layer on the top of SQLite. Room Database takes care of complicated queries and operations. It used annotations for creating tables, defining characteristics of the column, operations on the table like insert, delete, create and update. To use Room, create an abstract class extending RoomDatabase.


Add dependencies 

implementation ""

annotationProcessor ""

For more learning goto

Leave a Reply