Skip to content

Commit dcdff42

Browse files
author
Mahavir Jain
authored
Merge pull request #199 from codetoart/master
Fixed app crashing for android api verison < 26
2 parents dd6bb68 + 397af03 commit dcdff42

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

folioreader/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ext {
1515
siteUrl = 'https://github.com/FolioReader/FolioReader-Android'
1616
gitUrl = 'https://github.com/FolioReader/FolioReader-Android.git'
1717

18-
libraryVersion = '0.3.7'
18+
libraryVersion = '0.3.8'
1919

2020
developerId = 'mobisystech'
2121
developerName = 'Folio Reader'

folioreader/src/main/java/com/folioreader/FolioReader.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Context;
66
import android.content.Intent;
77
import android.content.IntentFilter;
8+
import android.os.Build;
89
import android.os.Parcelable;
910
import android.support.v4.content.LocalBroadcastManager;
1011

@@ -79,7 +80,7 @@ private FolioReader() {
7980

8081
private FolioReader(Context context) {
8182
this.context = context;
82-
new DbAdapter(context);
83+
DbAdapter.initialize(context);
8384
LocalBroadcastManager.getInstance(context).registerReceiver(highlightReceiver,
8485
new IntentFilter(HighlightImpl.BROADCAST_EVENT));
8586
LocalBroadcastManager.getInstance(context).registerReceiver(readPositionReceiver,
@@ -157,6 +158,8 @@ public FolioReader openBook(String assetOrSdcardPath, Config config, String book
157158
private Intent getIntentFromUrl(String assetOrSdcardPath, int rawId) {
158159

159160
Intent intent = new Intent(context, FolioActivity.class);
161+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M)
162+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
160163
intent.putExtra(FolioActivity.EXTRA_READ_POSITION, (Parcelable) readPosition);
161164

162165
if (rawId != 0) {
@@ -192,12 +195,37 @@ public void saveReceivedHighLights(List<HighLight> highlights, OnSaveHighlight o
192195
new SaveReceivedHighlightTask(onSaveHighlight, highlights).execute();
193196
}
194197

195-
public static void clear() {
198+
/**
199+
* Nullifies readPosition and listeners.
200+
* This method ideally should be used in onDestroy() of Activity or Fragment.
201+
* Use this method if you want to use FolioReader singleton instance again in the application,
202+
* else use {@link #stop()} which destruct the FolioReader singleton instance.
203+
*/
204+
public static synchronized void clear() {
196205

197206
if (singleton != null) {
198207
singleton.readPosition = null;
199208
singleton.onHighlightListener = null;
200209
singleton.readPositionListener = null;
201210
}
202211
}
212+
213+
/**
214+
* Destructs the FolioReader singleton instance.
215+
* Use this method only if you are sure that you won't need to use
216+
* FolioReader singleton instance again in application, else use {@link #clear()}.
217+
*/
218+
public static synchronized void stop() {
219+
220+
if (singleton != null) {
221+
DbAdapter.terminate();
222+
singleton.unregisterListeners();
223+
singleton = null;
224+
}
225+
}
226+
227+
private void unregisterListeners() {
228+
LocalBroadcastManager.getInstance(context).unregisterReceiver(highlightReceiver);
229+
LocalBroadcastManager.getInstance(context).unregisterReceiver(readPositionReceiver);
230+
}
203231
}

folioreader/src/main/java/com/folioreader/model/sqlite/DbAdapter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
public class DbAdapter {
1010
private static final String TAG = "DBAdapter";
1111

12-
private Context mContext;
1312
public static SQLiteDatabase mDatabase;
1413

15-
public DbAdapter(Context ctx) {
16-
this.mContext = ctx;
14+
public static void initialize(Context mContext) {
1715
mDatabase = FolioDatabaseHelper.getInstance(mContext).getMyWritableDatabase();
1816
}
1917

18+
public static void terminate() {
19+
FolioDatabaseHelper.clearInstance();
20+
}
21+
2022
public static boolean insert(String table, ContentValues contentValues) {
2123

2224
return mDatabase.insert(table, null, contentValues) > 0;

folioreader/src/main/java/com/folioreader/model/sqlite/FolioDatabaseHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static FolioDatabaseHelper getInstance(Context context) {
3030
return mInstance;
3131
}
3232

33+
public static void clearInstance() {
34+
mInstance = null;
35+
}
36+
3337
public SQLiteDatabase getMyWritableDatabase() {
3438
if ((myWritableDb == null) || (!myWritableDb.isOpen())) {
3539
myWritableDb = this.getWritableDatabase();

webViewMarker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ext {
1313
siteUrl = 'https://github.com/FolioReader/FolioReader-Android'
1414
gitUrl = 'https://github.com/FolioReader/FolioReader-Android.git'
1515

16-
libraryVersion = '0.3.7'
16+
libraryVersion = '0.3.8'
1717

1818
developerId = 'mobisystech'
1919
developerName = 'Folio Reader'

0 commit comments

Comments
 (0)