Keep scene after Activity restart
This commit is contained in:
1 parent
588b255788
commit
5a2a5b4289
8 files changed
+75
-161
No files matched your search
@@ -122,6 +122,10 @@ public final class ContentActivity extends StageActivity
|
||||
lockDrawer();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRebirth(Class clazz) {
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
|
||||
@@ -78,7 +78,6 @@ import com.hippo.widget.FloatingActionButton;
|
||||
import com.hippo.widget.recyclerview.EasyRecyclerView;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// TODO remeber the data in ContentHelper after screen dirction change
|
||||
// TODO Must refresh when change source
|
||||
@@ -199,27 +198,6 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
return LAUNCH_MODE_SINGLE_TOP;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onSave(Map<String, Object> transferStop) {
|
||||
super.onSave(transferStop);
|
||||
|
||||
transferStop.put("gallery_list_helper_data", mGalleryListHelper.copyData());
|
||||
transferStop.put("gallery_list_helper_save", mGalleryListHelper.save());
|
||||
transferStop.put("gallery_list_helper_listener", mGalleryListHelper.mListener);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected void onRestore(Map<String, Object> transferStop) {
|
||||
super.onRestore(transferStop);
|
||||
|
||||
mGalleryListHelper = new GalleryListHelper(getStageActivity(),
|
||||
(List<GalleryInfo>) transferStop.get("gallery_list_helper_data"),
|
||||
(int[]) transferStop.get("gallery_list_helper_save"),
|
||||
(GalleryListHelperSettable) transferStop.get("gallery_list_helper_listener"));
|
||||
}
|
||||
|
||||
private void handleAnnouncer(Announcer announcer) {
|
||||
int mode = MODE_HOMEPAGE;
|
||||
if (announcer != null) {
|
||||
@@ -255,6 +233,13 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
handleAnnouncer(announcer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRebirth(@Nullable Bundle savedInstanceState) {
|
||||
super.onRebirth(savedInstanceState);
|
||||
|
||||
mGalleryListHelper = new GalleryListHelper(getStageActivity(), mGalleryListHelper);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@@ -298,7 +283,7 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
mSearchLayout.setHelper(this);
|
||||
|
||||
// Content Layout
|
||||
// OnRestore may create new GalleryListHelper
|
||||
// onRebirth may create new GalleryListHelper
|
||||
if (mGalleryListHelper == null) {
|
||||
mGalleryListHelper = new GalleryListHelper(mActivity);
|
||||
}
|
||||
@@ -329,8 +314,8 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy(boolean willSave) {
|
||||
super.onDestroy(willSave);
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
// Avoid memory leak
|
||||
if (mGalleryListHelper.mListener != null) {
|
||||
@@ -362,10 +347,6 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
mFabLayout.setPadding(mFabLayout.getPaddingLeft(), mFabLayout.getPaddingTop(), mFabLayout.getPaddingRight(), mCornerFabOriginalBottom + b);
|
||||
}
|
||||
|
||||
private void setFabState(int fabState) {
|
||||
setFabState(fabState, true);
|
||||
}
|
||||
|
||||
private void setFabState(int fabState, boolean animation) {
|
||||
if (mFabState != fabState) {
|
||||
mFabState = fabState;
|
||||
@@ -861,13 +842,13 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
super(context);
|
||||
}
|
||||
|
||||
public GalleryListHelper(Context context, List<GalleryInfo> data,
|
||||
int[] save, GalleryListHelperSettable listener) {
|
||||
super(context, data, save);
|
||||
public GalleryListHelper(Context context, GalleryListHelper older) {
|
||||
super(context, older);
|
||||
// Update gallery listener
|
||||
mListener = listener;
|
||||
mListener = older.mListener;
|
||||
if (mListener != null) {
|
||||
mListener.setGalleryListHelper(this);
|
||||
older.mListener = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +143,17 @@ public class ContentLayout extends FrameLayout {
|
||||
private static final String STATE_KEY_RECYCLER_VIEW = "recycler_view";
|
||||
private static final String STATE_KEY_TIP_MESSAGE = "tip_message";
|
||||
|
||||
private static final String STATE_KEY_FIRST_PAGE = "first_page";
|
||||
private static final String STATE_KEY_LAST_PAGE = "last_page";
|
||||
private static final String STATE_KEY_FIRST_INDEX = "first_index";
|
||||
private static final String STATE_KEY_LAST_INDEX = "last_index";
|
||||
private static final String STATE_KEY_PAGE_COUNT = "page_count";
|
||||
private static final String STATE_KEY_CURRENT_PAGE = "current_page";
|
||||
private static final String STATE_KEY_PAGE_VOLUME = "page_volume";
|
||||
private static final String STATE_KEY_CURRENT_TASK_ID = "current_task_id";
|
||||
private static final String STATE_KEY_CURRENT_TASK_TYPR = "current_task_type";
|
||||
private static final String STATE_KEY_CURRENT_TASK_PAGE = "current_task_page";
|
||||
|
||||
public static final int TYPE_REFRESH = 0;
|
||||
public static final int TYPE_PRE_PAGE = 1;
|
||||
public static final int TYPE_PRE_PAGE_KEEP_POS = 2;
|
||||
@@ -247,35 +258,20 @@ public class ContentLayout extends FrameLayout {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ContentHelper(Context context, List<E> data, int[] save) {
|
||||
protected ContentHelper(Context context, ContentHelper older) {
|
||||
this(context);
|
||||
mData.addAll(data);
|
||||
mData.addAll(older.mData);
|
||||
|
||||
mFirstPage = save[0];
|
||||
mLastPage = save[1];
|
||||
mFirstIndex = save[2];
|
||||
mLastIndex = save[3];
|
||||
mPageCount = save[4];
|
||||
mCurrentPage = save[5];
|
||||
mPageVolume = save[6];
|
||||
mCurrentTaskId = save[7];
|
||||
mCurrentTaskType = save[8];
|
||||
mCurrentTaskPage = save[9];
|
||||
}
|
||||
|
||||
public int[] save() {
|
||||
int[] save = new int[10];
|
||||
save[0] = mFirstPage;
|
||||
save[1] = mLastPage;
|
||||
save[2] = mFirstIndex;
|
||||
save[3] = mLastIndex;
|
||||
save[4] = mPageCount;
|
||||
save[5] = mCurrentPage;
|
||||
save[6] = mPageVolume;
|
||||
save[7] = mCurrentTaskId;
|
||||
save[8] = mCurrentTaskType;
|
||||
save[9] = mCurrentTaskPage;
|
||||
return save;
|
||||
mFirstPage = older.mFirstPage;
|
||||
mLastPage = older.mLastPage;
|
||||
mFirstIndex = older.mFirstIndex;
|
||||
mLastIndex = older.mLastIndex;
|
||||
mPageCount = older.mPageCount;
|
||||
mCurrentPage = older.mCurrentPage;
|
||||
mPageVolume = older.mPageVolume;
|
||||
mCurrentTaskId = older.mCurrentTaskId;
|
||||
mCurrentTaskType = older.mCurrentTaskType;
|
||||
mCurrentTaskPage = older.mCurrentTaskPage;
|
||||
}
|
||||
|
||||
private void init(ContentLayout contentLayout) {
|
||||
@@ -328,12 +324,6 @@ public class ContentLayout extends FrameLayout {
|
||||
}
|
||||
}
|
||||
|
||||
public List<E> copyData() {
|
||||
List<E> data = new ArrayList<>(mData.size());
|
||||
data.addAll(mData);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException
|
||||
* if {@code location < 0 || location >= size()}
|
||||
|
||||
@@ -38,7 +38,7 @@ public abstract class Curtain {
|
||||
return scene != null && scene.getId() == mPreviousSceneId;
|
||||
}
|
||||
|
||||
protected void onReplace() {
|
||||
protected void onRebirth() {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,14 +33,13 @@ import android.widget.FrameLayout;
|
||||
import com.hippo.util.AssertUtils;
|
||||
import com.hippo.util.ViewUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link com.hippo.scene.Scene} is a {@code Activity} of {@link android.app.Activity}.
|
||||
* <p>
|
||||
* When start a new {@code Scene}, previous {@code Scene} can stay at screen for
|
||||
* a while. Or retain for a while before destroy.
|
||||
*/
|
||||
// TODO Improve state
|
||||
public abstract class Scene {
|
||||
|
||||
public static final int LAUNCH_MODE_STANDARD = 0;
|
||||
@@ -168,8 +167,8 @@ public abstract class Scene {
|
||||
}
|
||||
|
||||
// TODO Tell destory is it need close
|
||||
void destroy(boolean willSave) {
|
||||
onDestroy(willSave);
|
||||
void destroy() {
|
||||
onDestroy();
|
||||
|
||||
SceneApplication.getRefWatcher(getStageActivity()).watch(this);
|
||||
}
|
||||
@@ -190,12 +189,8 @@ public abstract class Scene {
|
||||
onResume();
|
||||
}
|
||||
|
||||
void save(Map<String, Object> transferStop) {
|
||||
onSave(transferStop);
|
||||
}
|
||||
|
||||
void restore(Map<String, Object> transferStop) {
|
||||
onRestore(transferStop);
|
||||
void rebirth(@Nullable Bundle savedInstanceState) {
|
||||
onRebirth(savedInstanceState);
|
||||
}
|
||||
|
||||
void setFitPaddingBottom(int b) {
|
||||
@@ -249,10 +244,12 @@ public abstract class Scene {
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
protected void onDestroy(boolean willSave) {
|
||||
protected void onDestroy() {
|
||||
// Make sure soft key broad is hidden
|
||||
InputMethodManager imm = (InputMethodManager) getStageActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.hideSoftInputFromWindow(mSceneView.getWindowToken(), 0);
|
||||
if (mSceneView != null) {
|
||||
InputMethodManager imm = (InputMethodManager) getStageActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.hideSoftInputFromWindow(mSceneView.getWindowToken(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected void onOpen() {
|
||||
@@ -269,18 +266,9 @@ public abstract class Scene {
|
||||
getSceneView().setEnableTouch(true);
|
||||
}
|
||||
|
||||
protected void onSave(Map<String, Object> transferStop) {
|
||||
transferStop.put("scene_id", mId);
|
||||
transferStop.put("scene_announcer", mAnnouncer);
|
||||
transferStop.put("scene_curtain", mCurtain);
|
||||
}
|
||||
|
||||
protected void onRestore(Map<String, Object> transferStop) {
|
||||
mId = (Integer) transferStop.get("scene_id");
|
||||
mAnnouncer = (Announcer) transferStop.get("scene_announcer");
|
||||
mCurtain = (Curtain) transferStop.get("scene_curtain");
|
||||
protected void onRebirth(@Nullable Bundle savedInstanceState) {
|
||||
if (mCurtain != null) {
|
||||
mCurtain.onReplace();
|
||||
mCurtain.onRebirth();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,9 +24,7 @@ import com.hippo.util.IntIdGenerator;
|
||||
import com.hippo.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
public class SceneManager {
|
||||
@@ -42,8 +40,6 @@ public class SceneManager {
|
||||
|
||||
private List<SceneStateListener> mSceneStateListenerList = new ArrayList<>();
|
||||
|
||||
private Map<String, Object> mTransferStop = new HashMap<>();
|
||||
|
||||
private static SceneManager sSceneManger;
|
||||
|
||||
public static SceneManager getInstance() {
|
||||
@@ -105,6 +101,12 @@ public class SceneManager {
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyRebirth(Class clazz) {
|
||||
for (SceneStateListener l : mSceneStateListenerList) {
|
||||
l.onRebirth(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
Scene createSceneByClass(@NonNull Class sceneClass) {
|
||||
try {
|
||||
return (Scene) sceneClass.newInstance();
|
||||
@@ -209,7 +211,7 @@ public class SceneManager {
|
||||
// It is the last scene, just finish the activity
|
||||
mSceneStack.remove(index);
|
||||
|
||||
scene.destroy(false);
|
||||
scene.destroy();
|
||||
scene.setState(Scene.SCENE_STATE_DESTROY);
|
||||
notifyDestroy(scene.getClass());
|
||||
|
||||
@@ -228,7 +230,7 @@ public class SceneManager {
|
||||
notifyResume(previousState.getClass());
|
||||
}
|
||||
|
||||
scene.destroy(false);
|
||||
scene.destroy();
|
||||
scene.setState(Scene.SCENE_STATE_DESTROY);
|
||||
notifyDestroy(scene.getClass());
|
||||
|
||||
@@ -293,32 +295,12 @@ public class SceneManager {
|
||||
}
|
||||
|
||||
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
|
||||
Stack<Scene> tempStack = new Stack<>();
|
||||
tempStack.addAll(mSceneStack);
|
||||
mSceneStack.clear();
|
||||
|
||||
Map<String, Object> transferStop = mTransferStop;
|
||||
for (Scene oldScene : tempStack) {
|
||||
oldScene.destroy(true);
|
||||
oldScene.setState(Scene.SCENE_STATE_DESTROY);
|
||||
notifyDestroy(oldScene.getClass());
|
||||
|
||||
oldScene.save(transferStop);
|
||||
|
||||
Scene newScene = createSceneByClass(oldScene.getClass());
|
||||
|
||||
newScene.restore(transferStop);
|
||||
|
||||
newScene.create(savedInstanceState);
|
||||
newScene.setState(Scene.SCENE_STATE_CREATE);
|
||||
notifyCreate(newScene.getClass());
|
||||
|
||||
newScene.restoreInstanceState(savedInstanceState);
|
||||
|
||||
newScene.setState(Scene.SCENE_STATE_RUN);
|
||||
mSceneStack.push(newScene);
|
||||
|
||||
transferStop.clear();
|
||||
for (Scene scene : mSceneStack) {
|
||||
// Recreate
|
||||
scene.rebirth(savedInstanceState);
|
||||
notifyRebirth(scene.getClass());
|
||||
scene.create(savedInstanceState);
|
||||
scene.restoreInstanceState(savedInstanceState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,5 +309,6 @@ public class SceneManager {
|
||||
void onDestroy(Class clazz);
|
||||
void onPause(Class clazz);
|
||||
void onResume(Class clazz);
|
||||
void onRebirth(Class clazz);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,6 @@ import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.Space;
|
||||
@@ -38,8 +37,6 @@ import com.hippo.effect.ripple.RippleSalon;
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.util.ViewUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
SceneDialogView.OnClickOutOfDialogListener, AdapterView.OnItemClickListener {
|
||||
|
||||
@@ -106,28 +103,8 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSave(Map<String, Object> transferStop) {
|
||||
super.onSave(transferStop);
|
||||
|
||||
transferStop.put("builder", mBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestore(Map<String, Object> transferStop) {
|
||||
super.onRestore(transferStop);
|
||||
|
||||
mBuilder = (Builder) transferStop.get("builder");
|
||||
// Create new Adapter to avoid memory leak
|
||||
if (mBuilder.mAdapter != null) {
|
||||
mBuilder.mAdapter = new ArrayAdapter<>(getStageActivity(),
|
||||
mBuilder.mItemLayoutResId, android.R.id.text1,
|
||||
mBuilder.mItems);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy(boolean willSave) {
|
||||
super.onDestroy(willSave);
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
OnCloseListener listener = mBuilder.mOnCloseListener;
|
||||
if (listener != null) {
|
||||
@@ -152,8 +129,9 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
mMessage.setText(mBuilder.mMessage);
|
||||
ViewUtils.setVisibility(mListView, View.GONE);
|
||||
ViewUtils.setVisibility(mCustom, View.GONE);
|
||||
} else if (mBuilder.mAdapter != null) {
|
||||
listView.setAdapter(mBuilder.mAdapter);
|
||||
} else if (mBuilder.mItemLayoutResId != 0) {
|
||||
listView.setAdapter(new ArrayAdapter<>(getStageActivity(),
|
||||
mBuilder.mItemLayoutResId, android.R.id.text1, mBuilder.mItems));
|
||||
listView.setOnItemClickListener(this);
|
||||
if (mBuilder.mIsSingleChoice) {
|
||||
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
|
||||
@@ -319,7 +297,6 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
|
||||
private CharSequence[] mItems;
|
||||
private int mItemLayoutResId;
|
||||
private ListAdapter mAdapter;
|
||||
private OnClickListener mOnListItemClickListener;
|
||||
protected int mCheckedItem;
|
||||
protected boolean mIsSingleChoice;
|
||||
@@ -362,8 +339,6 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
public Builder setItems(int itemsId, final OnClickListener listener) {
|
||||
mItems = mContext.getResources().getTextArray(itemsId);
|
||||
mItemLayoutResId = R.layout.select_dialog_item;
|
||||
mAdapter = new ArrayAdapter<>(mContext, R.layout.select_dialog_item,
|
||||
android.R.id.text1, mItems);
|
||||
mOnListItemClickListener = listener;
|
||||
mIsSingleChoice = false;
|
||||
return this;
|
||||
@@ -373,8 +348,6 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
final OnClickListener listener) {
|
||||
mItems = items;
|
||||
mItemLayoutResId = R.layout.select_dialog_item;
|
||||
mAdapter = new ArrayAdapter<>(mContext, R.layout.select_dialog_item,
|
||||
android.R.id.text1, items);
|
||||
mOnListItemClickListener = listener;
|
||||
mIsSingleChoice = false;
|
||||
return this;
|
||||
@@ -384,9 +357,6 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
final OnClickListener listener) {
|
||||
mItems = mContext.getResources().getTextArray(itemsId);
|
||||
mItemLayoutResId = R.layout.select_dialog_singlechoice;
|
||||
mAdapter = new ArrayAdapter<>(mContext,
|
||||
R.layout.select_dialog_singlechoice, android.R.id.text1,
|
||||
mItems);
|
||||
mOnListItemClickListener = listener;
|
||||
mCheckedItem = checkedItem;
|
||||
mIsSingleChoice = true;
|
||||
@@ -397,8 +367,6 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
|
||||
int checkedItem, final OnClickListener listener) {
|
||||
mItems = items;
|
||||
mItemLayoutResId = R.layout.select_dialog_singlechoice;
|
||||
mAdapter = new ArrayAdapter<>(mContext,
|
||||
R.layout.select_dialog_singlechoice, android.R.id.text1, items);
|
||||
mOnListItemClickListener = listener;
|
||||
mCheckedItem = checkedItem;
|
||||
mIsSingleChoice = true;
|
||||
|
||||
@@ -57,7 +57,7 @@ public class SimpleDialogCurtain extends Curtain {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReplace() {
|
||||
protected void onRebirth() {
|
||||
mStartX = 0;
|
||||
mStartY = 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user