Remove all the other scene when click Homepage

This commit is contained in:
seven332 committed 2016-02-05 14:37:18 +08:00
1 parent 8387ff3fc6
commit a61eb49b27
4 files changed
+79 -25

No files matched your search

@@ -30,6 +30,7 @@ import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.ui.scene.GalleryListScene;
import com.hippo.ehviewer.ui.scene.LoginScene;
import com.hippo.ehviewer.ui.scene.WarningScene;
import com.hippo.scene.SceneFragment;
import com.hippo.scene.StageActivity;
public final class MainActivity extends StageActivity
@@ -114,7 +115,7 @@ public final class MainActivity extends StageActivity
if (id == R.id.nav_homepage) {
Bundle args = new Bundle();
args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_HOMEPAGE);
startScene(GalleryListScene.class, args);
startScene(GalleryListScene.class, args, null, SceneFragment.FLAG_REMOVE_ALL_THE_OTHER_SCENES);
} else if (id == R.id.nav_whats_hot) {
} else if (id == R.id.nav_favourite) {
@@ -38,6 +38,8 @@ public class SceneFragment extends Fragment {
public static final int LAUNCH_MODE_STANDARD = 0;
public static final int LAUNCH_MODE_SINGLE_TOP = 1;
public static final int FLAG_REMOVE_ALL_THE_OTHER_SCENES = 0x1;
@LaunchMode
public int getLaunchMode() {
return LAUNCH_MODE_STANDARD;
@@ -51,21 +53,27 @@ public class SceneFragment extends Fragment {
public void onNewArguments(@NonNull Bundle args) {}
public <T extends SceneFragment> void startScene(Class<T> clazz) {
startScene(clazz, null, null);
startScene(clazz, null, null, 0);
}
public <T extends SceneFragment> void startScene(Class<T> clazz, Bundle args) {
startScene(clazz, args, null);
startScene(clazz, args, null, 0);
}
public <T extends SceneFragment> void startScene(Class<T> clazz, TransitionHelper transitionHelper) {
startScene(clazz, null, transitionHelper);
startScene(clazz, null, transitionHelper, 0);
}
public <T extends SceneFragment> void startScene(Class<T> clazz, Bundle args, TransitionHelper transitionHelper) {
public <T extends SceneFragment> void startScene(Class<T> clazz, Bundle args,
TransitionHelper transitionHelper) {
startScene(clazz, args, transitionHelper, 0);
}
public <T extends SceneFragment> void startScene(Class<T> clazz, Bundle args,
TransitionHelper transitionHelper, int flag) {
FragmentActivity activity = getActivity();
if (activity instanceof StageActivity) {
((StageActivity) activity).startScene(clazz, args, transitionHelper);
((StageActivity) activity).startScene(clazz, args, transitionHelper, flag);
}
}
@@ -122,22 +122,25 @@ public abstract class StageActivity extends AppCompatActivity {
}
public void startScene(Class<?> clazz) {
startScene(clazz, null, null);
startScene(clazz, null, null, 0);
}
public void startScene(Class<?> clazz, Bundle args) {
startScene(clazz, args, null);
startScene(clazz, args, null, 0);
}
public void startScene(Class<?> clazz, TransitionHelper transitionHelper) {
startScene(clazz, null, transitionHelper);
startScene(clazz, null, transitionHelper, 0);
}
public void startScene(Class<?> clazz,
Bundle args, TransitionHelper transitionHelper) {
Bundle args, TransitionHelper transitionHelper, int flag) {
boolean createNewScene = true;
boolean removeAllTheOthers = (flag & SceneFragment.FLAG_REMOVE_ALL_THE_OTHER_SCENES) != 0;
FragmentManager fragmentManager = getSupportFragmentManager();
// Old scene
// Current fragment
Fragment currentFragment = null;
if (mSceneTagList.size() > 0) {
// Get last tag
@@ -149,22 +152,42 @@ public abstract class StageActivity extends AppCompatActivity {
if (currentFragment instanceof SceneFragment && clazz.isInstance(currentFragment)) {
SceneFragment currentScene = (SceneFragment) currentFragment;
if (currentScene.getLaunchMode() == SceneFragment.LAUNCH_MODE_SINGLE_TOP) {
createNewScene = false;
if (args != null) {
currentScene.onNewArguments(args);
}
return;
if (!removeAllTheOthers) {
// Done!
return;
}
}
}
// Create new scene
SceneFragment newScene = newSceneInstance(clazz);
newScene.setArguments(args);
SceneFragment newScene = null;
String newTag = null;
if (createNewScene) {
// Create new scene
newScene = newSceneInstance(clazz);
newScene.setArguments(args);
// Create new scene tag
String newTag = Integer.toString(mIdGenerator.nextId());
// Create new scene tag
newTag = Integer.toString(mIdGenerator.nextId());
// Add new tag to list
mSceneTagList.add(newTag);
}
// 1. createNewScene false, removeAllTheOthers false
// Will not go here
// 2. createNewScene false, removeAllTheOthers true
// Keep current scene, remove all the others, no animation
// 3. createNewScene true, removeAllTheOthers false
// Add new scene, detach current fragment, with animation if exist currentFragment
// 4. createNewScene true, removeAllTheOthers true
// Add new scene, remove all the others, with animation if exist currentFragment
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (currentFragment != null) {
// Animation
if (newScene != null && currentFragment != null) {
if (transitionHelper == null || !transitionHelper.onTransition(
this, transaction, currentFragment, newScene)) {
// Clear shared item
@@ -179,16 +202,39 @@ public abstract class StageActivity extends AppCompatActivity {
// Set default animation
transaction.setCustomAnimations(R.anim.scene_open_enter, R.anim.scene_open_exit);
}
}
// Remove scene
if (removeAllTheOthers) {
int startIndex = mSceneTagList.size() - 2;
// Remove scene
for (int i = startIndex; i >= 0; i--) {
String tag = mSceneTagList.get(i);
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
transaction.remove(fragment);
} else {
Log.d(TAG, "Can't find fragment with tag: " + tag);
}
}
// Remove scene tag
if (startIndex >= 0) {
mSceneTagList.subList(0, startIndex + 1).clear();
}
}
// Detach
if (!removeAllTheOthers && currentFragment != null) {
transaction.detach(currentFragment);
}
transaction.add(getContainerViewId(), newScene, newTag);
// Add
if (newScene != null) {
transaction.add(getContainerViewId(), newScene, newTag);
}
transaction.commit();
// Add new tag to list
mSceneTagList.add(newTag);
// Update SoftInputMode
getWindow().setSoftInputMode(newScene.getSoftInputMode());
if (newScene != null) {
getWindow().setSoftInputMode(newScene.getSoftInputMode());
}
}
int getStackIndex(SceneFragment scene) {