|
5 | 5 | import android.content.Context; |
6 | 6 | import android.content.Intent; |
7 | 7 | import android.content.IntentFilter; |
| 8 | +import android.os.Build; |
8 | 9 | import android.os.Parcelable; |
9 | 10 | import android.support.v4.content.LocalBroadcastManager; |
10 | 11 |
|
@@ -79,7 +80,7 @@ private FolioReader() { |
79 | 80 |
|
80 | 81 | private FolioReader(Context context) { |
81 | 82 | this.context = context; |
82 | | - new DbAdapter(context); |
| 83 | + DbAdapter.initialize(context); |
83 | 84 | LocalBroadcastManager.getInstance(context).registerReceiver(highlightReceiver, |
84 | 85 | new IntentFilter(HighlightImpl.BROADCAST_EVENT)); |
85 | 86 | LocalBroadcastManager.getInstance(context).registerReceiver(readPositionReceiver, |
@@ -157,6 +158,8 @@ public FolioReader openBook(String assetOrSdcardPath, Config config, String book |
157 | 158 | private Intent getIntentFromUrl(String assetOrSdcardPath, int rawId) { |
158 | 159 |
|
159 | 160 | 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); |
160 | 163 | intent.putExtra(FolioActivity.EXTRA_READ_POSITION, (Parcelable) readPosition); |
161 | 164 |
|
162 | 165 | if (rawId != 0) { |
@@ -192,12 +195,37 @@ public void saveReceivedHighLights(List<HighLight> highlights, OnSaveHighlight o |
192 | 195 | new SaveReceivedHighlightTask(onSaveHighlight, highlights).execute(); |
193 | 196 | } |
194 | 197 |
|
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() { |
196 | 205 |
|
197 | 206 | if (singleton != null) { |
198 | 207 | singleton.readPosition = null; |
199 | 208 | singleton.onHighlightListener = null; |
200 | 209 | singleton.readPositionListener = null; |
201 | 210 | } |
202 | 211 | } |
| 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 | + } |
203 | 231 | } |
0 commit comments