Last active
May 30, 2017 19:30
-
-
Save reline/70af1b2736502d7637c56ec5cf92839c to your computer and use it in GitHub Desktop.
A controller manager for the Conductor library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.os.Parcelable; | |
import android.support.annotation.NonNull; | |
import android.util.SparseArray; | |
import android.view.ViewGroup; | |
import com.bluelinelabs.conductor.Controller; | |
import com.bluelinelabs.conductor.Router; | |
import com.bluelinelabs.conductor.RouterTransaction; | |
public class ControllerManager { | |
private static final String KEY_SAVED_PAGES = "ControllerManager.savedStates"; | |
private static final String KEY_CURRENT_ROUTER_ID = "ControllerManager.routerId"; | |
private final Controller host; | |
private final ViewGroup container; | |
private SparseArray<Bundle> savedPages = new SparseArray<>(); | |
private int currentRouterId = -1; | |
public ControllerManager(@NonNull Controller host, ViewGroup container) { | |
this.host = host; | |
this.container = container; | |
} | |
public void show(@NonNull Controller controller, int id) { | |
final String currentRouterTag = getRouterTag(currentRouterId); | |
if (!getRouterTag(id).equals(currentRouterTag)) { | |
if (currentRouterId != -1) { | |
destroyItem(currentRouterId, host.getChildRouter(container, currentRouterTag)); | |
} | |
instantiateItem(controller, id); | |
} | |
} | |
private void instantiateItem(Controller controller, int id) { | |
final String tag = getRouterTag(id); | |
Router router = host.getChildRouter(container, tag); | |
if (!router.hasRootController()) { | |
Bundle routerSavedState = savedPages.get(id); | |
if (routerSavedState != null) { | |
router.restoreInstanceState(routerSavedState); | |
} | |
} | |
router.rebindIfNeeded(); | |
if (!router.hasRootController()) { | |
router.setRoot(RouterTransaction.with(controller)); | |
} | |
currentRouterId = id; | |
} | |
private void destroyItem(int id, Router router) { | |
Bundle savedState = new Bundle(); | |
router.saveInstanceState(savedState); | |
savedPages.put(id, savedState); | |
host.removeChildRouter(router); | |
} | |
public Parcelable saveState() { | |
Bundle bundle = new Bundle(); | |
bundle.putSparseParcelableArray(KEY_SAVED_PAGES, savedPages); | |
bundle.putInt(KEY_CURRENT_ROUTER_ID, currentRouterId); | |
return bundle; | |
} | |
public void restoreState(Parcelable state) { | |
Bundle bundle = (Bundle) state; | |
if (state != null) { | |
savedPages = bundle.getSparseParcelableArray(KEY_SAVED_PAGES); | |
currentRouterId = bundle.getInt(KEY_CURRENT_ROUTER_ID, -1); | |
} | |
} | |
private String getRouterTag(int id) { | |
return container.getId() + ":" + id; | |
} | |
public boolean handleBack() { | |
return host.getChildRouter(container, getRouterTag(currentRouterId)).handleBack(); | |
} | |
} |
I don't have any projects I can share, but it is currently being used in production code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks nice!
Are there any samples where you have used this?