diff --git a/conductor/src/main/java/com/bluelinelabs/conductor/ControllerChangeHandler.java b/conductor/src/main/java/com/bluelinelabs/conductor/ControllerChangeHandler.java index c8538452..a30b40cb 100644 --- a/conductor/src/main/java/com/bluelinelabs/conductor/ControllerChangeHandler.java +++ b/conductor/src/main/java/com/bluelinelabs/conductor/ControllerChangeHandler.java @@ -28,8 +28,7 @@ public abstract class ControllerChangeHandler { private static final Map inProgressPushHandlers = new HashMap<>(); private boolean forceRemoveViewOnPush; - - boolean hasBeenUsed; + private boolean hasBeenUsed; /** * Responsible for swapping Views from one Controller to another. @@ -85,6 +84,16 @@ public ControllerChangeHandler copy() { return fromBundle(toBundle()); } + /** + * Returns whether or not this is a reusable ControllerChangeHandler. Defaults to false and should + * ONLY be overridden if there are absolutely no side effects to using this handler more than once. + * In the case that a handler is not reusable, it will be copied using the {@link #copy()} method + * prior to use. + */ + public boolean isReusable() { + return false; + } + @NonNull final Bundle toBundle() { Bundle bundle = new Bundle(); @@ -145,7 +154,7 @@ public static void executeChange(@Nullable final Controller to, @Nullable final final ControllerChangeHandler handler; if (inHandler == null) { handler = new SimpleSwapChangeHandler(); - } else if (inHandler.hasBeenUsed) { + } else if (inHandler.hasBeenUsed && !inHandler.isReusable()) { handler = inHandler.copy(); } else { handler = inHandler; diff --git a/conductor/src/main/java/com/bluelinelabs/conductor/changehandler/SimpleSwapChangeHandler.java b/conductor/src/main/java/com/bluelinelabs/conductor/changehandler/SimpleSwapChangeHandler.java index 7a27f1d9..5d845511 100755 --- a/conductor/src/main/java/com/bluelinelabs/conductor/changehandler/SimpleSwapChangeHandler.java +++ b/conductor/src/main/java/com/bluelinelabs/conductor/changehandler/SimpleSwapChangeHandler.java @@ -108,4 +108,8 @@ public ControllerChangeHandler copy() { return new SimpleSwapChangeHandler(removesFromViewOnPush()); } + @Override + public boolean isReusable() { + return true; + } } diff --git a/conductor/src/main/java/com/bluelinelabs/conductor/internal/NoOpControllerChangeHandler.java b/conductor/src/main/java/com/bluelinelabs/conductor/internal/NoOpControllerChangeHandler.java index b429ac6c..838174f1 100644 --- a/conductor/src/main/java/com/bluelinelabs/conductor/internal/NoOpControllerChangeHandler.java +++ b/conductor/src/main/java/com/bluelinelabs/conductor/internal/NoOpControllerChangeHandler.java @@ -19,4 +19,9 @@ public void performChange(@NonNull ViewGroup container, @Nullable View from, @Nu public ControllerChangeHandler copy() { return new NoOpControllerChangeHandler(); } + + @Override + public boolean isReusable() { + return true; + } } \ No newline at end of file diff --git a/conductor/src/test/java/com/bluelinelabs/conductor/MockChangeHandler.java b/conductor/src/test/java/com/bluelinelabs/conductor/MockChangeHandler.java index 703bf7e5..2e652abd 100644 --- a/conductor/src/test/java/com/bluelinelabs/conductor/MockChangeHandler.java +++ b/conductor/src/test/java/com/bluelinelabs/conductor/MockChangeHandler.java @@ -1,6 +1,5 @@ package com.bluelinelabs.conductor; - import android.os.Bundle; import android.support.annotation.NonNull; import android.view.View; @@ -82,4 +81,15 @@ public void restoreFromBundle(@NonNull Bundle bundle) { super.restoreFromBundle(bundle); removesFromViewOnPush = bundle.getBoolean(KEY_REMOVES_FROM_VIEW_ON_PUSH); } + + @NonNull + @Override + public ControllerChangeHandler copy() { + return new MockChangeHandler(removesFromViewOnPush, listener); + } + + @Override + public boolean isReusable() { + return true; + } }