diff --git a/app/src/main/java/org/bspeice/minimalbible/OGHolder.java b/app/src/main/java/org/bspeice/minimalbible/OGHolder.java new file mode 100644 index 0000000..6eafc42 --- /dev/null +++ b/app/src/main/java/org/bspeice/minimalbible/OGHolder.java @@ -0,0 +1,50 @@ +package org.bspeice.minimalbible; + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; + +import java.util.HashMap; +import java.util.Map; + +import dagger.ObjectGraph; + +/** + * Holding location for activity object graphs. + * This technique could be extended to other things, but honestly, + * everything it could be extended to likely needs to be in + * an ObjectGraph anyway. + * This works because getSupportFragmentManager() is scoped to each activity. + * TODO: Prove the above claim. + */ +public class OGHolder extends Fragment { + private final static String TAG = "OGHolder"; + + private ObjectGraph mObjectGraph; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setRetainInstance(true); + } + + public void persistGraph(ObjectGraph graph) { + mObjectGraph = graph; + } + + public ObjectGraph fetchGraph() { + return mObjectGraph; + } + + public static OGHolder get(FragmentActivity activity) { + FragmentManager manager = activity.getSupportFragmentManager(); + OGHolder holder = (OGHolder) manager.findFragmentByTag(TAG); + if (holder == null) { + holder = new OGHolder(); + manager.beginTransaction().add(holder, TAG).commit(); + } + return holder; + } +} diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivity.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivity.java index 16c00e1..ad55c73 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivity.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivity.java @@ -10,6 +10,7 @@ import android.view.MenuItem; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.MinimalBible; +import org.bspeice.minimalbible.OGHolder; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; @@ -26,6 +27,8 @@ public class DownloadActivity extends BaseActivity implements BaseNavigationDrawerFragment.NavigationDrawerCallbacks, Injector { + private final String TAG = "DownloadActivity"; + /** * Fragment managing the behaviors, interactions and presentation of the * navigation drawer. @@ -48,19 +51,15 @@ public class DownloadActivity extends BaseActivity implements */ private void buildObjGraph() { if (daObjectGraph == null) { - FragmentManager fM = getSupportFragmentManager(); - DownloadOGHolder ogHolder = (DownloadOGHolder) fM.findFragmentByTag("OG_HOLDER"); - if (ogHolder == null) { - Log.e("DownloadActivity", "Creating new holder..."); - ogHolder = new DownloadOGHolder(); + OGHolder holder = OGHolder.get(this); + ObjectGraph holderGraph = holder.fetchGraph(); + if (holderGraph == null) { + Log.i(TAG, "Rebuilding ObjectGraph..."); daObjectGraph = MinimalBible.get(this) .plus(new DownloadActivityModules(this)); - ogHolder.persistObjectGraph(daObjectGraph); - fM.beginTransaction().add(ogHolder, "OG_HOLDER").commit(); + holder.persistGraph(daObjectGraph); } else { - Log.e("DownloadActivity", "Found existing holder..."); - daObjectGraph = ogHolder.fetchObjectGraph(); - daObjectGraph.inject(this); + daObjectGraph = holderGraph; } } daObjectGraph.inject(this); diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadOGHolder.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadOGHolder.java deleted file mode 100644 index 586c9de..0000000 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadOGHolder.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.bspeice.minimalbible.activity.downloader; - -import android.os.Bundle; -import android.support.v4.app.Fragment; - -import dagger.ObjectGraph; - -/** - * Created by bspeice on 7/21/14. - */ -public class DownloadOGHolder extends Fragment { - - ObjectGraph holder; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setRetainInstance(true); - } - - public void persistObjectGraph(ObjectGraph holder) { - this.holder = holder; - } - - public ObjectGraph fetchObjectGraph() { - return this.holder; - } -}