Skip to content

Commit

Permalink
Only reinit collection when needed, Fixes #4938
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed Sep 13, 2018
1 parent 55fbac6 commit 7129244
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.ichi2.libanki.Collection;
import com.ichi2.themes.Themes;

import java.util.concurrent.atomic.AtomicBoolean;

import timber.log.Timber;

public class AnkiActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Collection>,
Expand All @@ -44,6 +46,15 @@ public class AnkiActivity extends AppCompatActivity implements LoaderManager.Loa
public final int SIMPLE_NOTIFICATION_ID = 0;
public static final int REQUEST_REVIEW = 901;

/** Do we need to notify callbacks from the {@link #onLoadFinished(Loader, Collection)} method? */
private final AtomicBoolean mNotifyLoaderCallbacks = new AtomicBoolean(true);

/**
* Is the collection initialized? if this is false, you need to reinitialize
* From MVP template https://github.com/benoitletondor/Android-Studio-MVP-template/blob/master/MVPBoilerplate/root/src/app_package/classes/BaseActivity.java.ftl#L58
*/
protected AtomicBoolean mNeedToInitializeCollection = new AtomicBoolean(true);

private DialogHandler mHandler = new DialogHandler(this);

// custom tabs
Expand All @@ -64,6 +75,11 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onStart() {
super.onStart();
mCustomTabActivityHelper.bindCustomTabsService(this);
if (mNeedToInitializeCollection.get()) {
Timber.d("Still need to initialize collection with callbacks");
mNotifyLoaderCallbacks.set(true);
}
mNeedToInitializeCollection.set(false);
}

@Override
Expand Down Expand Up @@ -280,7 +296,10 @@ public Loader<Collection> onCreateLoader(int id, Bundle args) {
public void onLoadFinished(Loader<Collection> loader, Collection col) {
hideProgressBar();
if (col != null && colIsOpen()) {
onCollectionLoaded(col);
if (mNotifyLoaderCallbacks.compareAndSet(true, false)) {
Timber.d("Callbacks need notification, calling onCollectionLoaded()");
onCollectionLoaded(col);
}
} else {
onCollectionLoadError();
}
Expand All @@ -289,7 +308,7 @@ public void onLoadFinished(Loader<Collection> loader, Collection col) {

@Override
public void onLoaderReset(Loader<Collection> arg0) {
// We don't currently retain any references, so no need to free any data here
mNeedToInitializeCollection.set(true);
}


Expand Down

0 comments on commit 7129244

Please # to comment.