aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java36
-rw-r--r--main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java19
-rw-r--r--main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java36
3 files changed, 89 insertions, 2 deletions
diff --git a/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java b/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java
index 952726e..e6167fb 100644
--- a/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java
+++ b/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java
@@ -9,6 +9,7 @@ import com.viewpagerindicator.TitleProvider;
import org.apache.commons.lang3.tuple.Pair;
import android.app.Activity;
+import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
@@ -43,6 +44,10 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
private final Map<Page, PageViewCreator> viewCreators = new HashMap<Page, PageViewCreator>();
/**
+ * Store the states of the page views to be able to persist them when destroyed and reinstantiated again
+ */
+ private final Map<Page, Bundle> viewStates = new HashMap<Page, Bundle>();
+ /**
* The {@link ViewPager} for this activity.
*/
private ViewPager viewPager;
@@ -76,6 +81,16 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
* Handles changed data-sets.
*/
public void notifyDataSetChanged();
+
+ /**
+ * Gets state of the view
+ */
+ public Bundle getViewState();
+
+ /**
+ * Set the state of the view
+ */
+ public void setViewState(Bundle state);
}
/**
@@ -93,6 +108,14 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
+
+ final Page page = pageOrder.get(position);
+
+ // Store the state of the view if the page supports it
+ PageViewCreator creator = viewCreators.get(page);
+ Bundle state = creator.getViewState();
+ viewStates.put(page, state);
+
container.removeView((View) object);
}
@@ -107,6 +130,7 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
@Override
public Object instantiateItem(ViewGroup container, int position) {
+
final Page page = pageOrder.get(position);
PageViewCreator creator = viewCreators.get(page);
@@ -114,6 +138,7 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
if (null == creator && null != page) {
creator = AbstractViewPagerActivity.this.createViewCreator(page);
viewCreators.put(page, creator);
+ viewStates.put(page, new Bundle());
}
View view = null;
@@ -123,12 +148,16 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
// Result from getView() is maybe cached, but it should be valid because the
// creator should be informed about data-changes with notifyDataSetChanged()
view = creator.getView();
+
+ // Restore the state of the view if the page supports it
+ Bundle state = viewStates.get(page);
+ creator.setViewState(state);
+
container.addView(view, 0);
}
} catch (Exception e) {
Log.e("ViewPagerAdapter.instantiateItem ", e);
}
-
return view;
}
@@ -225,10 +254,15 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends
protected abstract String getTitle(Page page);
protected final void reinitializeViewPager() {
+
// notify all creators that the data has changed
for (PageViewCreator creator : viewCreators.values()) {
creator.notifyDataSetChanged();
}
+ // reset the stored view states of all pages
+ for (Bundle state : viewStates.values()) {
+ state.clear();
+ }
pageOrder.clear();
final Pair<List<? extends Page>, Integer> pagesAndIndex = getOrderedPages();
diff --git a/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java b/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java
index 333ef11..55b153b 100644
--- a/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java
+++ b/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java
@@ -2,6 +2,7 @@ package cgeo.geocaching.ui;
import cgeo.geocaching.activity.AbstractViewPagerActivity.PageViewCreator;
+import android.os.Bundle;
import android.view.View;
/**
@@ -29,4 +30,22 @@ public abstract class AbstractCachingPageViewCreator<ViewClass extends View> imp
@Override
public abstract ViewClass getDispatchedView();
+
+ /**
+ * Gets the state of the view but returns an empty state if not overridden
+ *
+ * @return empty bundle
+ */
+ @Override
+ public Bundle getViewState() {
+ return new Bundle();
+ }
+
+ /**
+ * Restores the state of the view but just returns if not overridden.
+ */
+ @Override
+ public void setViewState(Bundle state) {
+ return;
+ }
}
diff --git a/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java b/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java
index 8fe3866..0a327f3 100644
--- a/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java
+++ b/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java
@@ -11,6 +11,7 @@ import cgeo.geocaching.ui.UserActionsClickListener;
import org.apache.commons.lang3.StringUtils;
import android.content.res.Resources;
+import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
@@ -109,4 +110,37 @@ public class CacheLogsViewCreator extends LogsViewCreator {
return new UserActionsClickListener(getCache());
}
-} \ No newline at end of file
+ /**
+ * Get the state of the current view
+ *
+ * @return the state encapsulated in a bundle
+ */
+ @Override
+ public Bundle getViewState() {
+ if (view == null) {
+ return null;
+ }
+ int position = view.getFirstVisiblePosition();
+ View child = view.getChildAt(0);
+ int positionFromTop = (child == null) ? 0 : child.getTop();
+ Bundle state = new Bundle();
+ state.putInt("position", position);
+ state.putInt("positionFromTop", positionFromTop);
+ return state;
+ }
+
+ /**
+ * Restore a previously stored state of the view
+ *
+ */
+ @Override
+ public void setViewState(Bundle state) {
+ if (view == null) {
+ return;
+ }
+ int logViewPosition = state.getInt("position");
+ int logViewPositionFromTop = state.getInt("positionFromTop");
+ view.setSelectionFromTop(logViewPosition, logViewPositionFromTop);
+ return;
+ }
+}