How to ensure that buffered data is not lost during configuration changes? Android destroys unused activities when you switch to another activities and destroys and recreates the activity if you change the orientation. If you have buffered any data in your activities it will be lost during these processes.
One technique to ensure data retention is to buffer it in your custom application class. Until the app is killed data in application class will not be destroyed.
Step 1: create MyApplication class
MyApplication.java
The singleton technique allows you to get reference of the application class anywhere in the app. Observe how items is declared and used.
Step 2: Declare your app has a custom application class in AndroidManifest.xml. See line 5.
AndroidManifest.xml
Databases and app performance
Along with data retention feature application methods like onCreate() will be run only once in the application life cycle. This will allow you to buffer the database records in the application class. This will give you faster access to database records and you will only need to write the changes to database when the app is stopped.
This minimal contact with database and buffering will improve your app's performance and give you a responsive app.
One technique to ensure data retention is to buffer it in your custom application class. Until the app is killed data in application class will not be destroyed.
Step 1: create MyApplication class
MyApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.example.mytodolist; public class MyApplication extends Application { private String[] items; public static MyApplication getInstance () { return singleton; } @Override public void onCreate() { super.onCreate(); singleton = this; items = new String[10]; } public String[] getItems () { return items; } } |
The singleton technique allows you to get reference of the application class anywhere in the app. Observe how items is declared and used.
Step 2: Declare your app has a custom application class in AndroidManifest.xml. See line 5.
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mytodolist" > <application android:name="com.example.mytodolist.MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Databases and app performance
Along with data retention feature application methods like onCreate() will be run only once in the application life cycle. This will allow you to buffer the database records in the application class. This will give you faster access to database records and you will only need to write the changes to database when the app is stopped.
This minimal contact with database and buffering will improve your app's performance and give you a responsive app.