Repository: eventfahrplan/eventfahrplan
Issue: List of favorites scrolls to the top when returning from details screen

# Environment
- App version: [v.1.49.0](https://github.com/EventFahrplan/EventFahrplan/releases/tag/v.1.49.0)
- Android version: all
- Branch: `master`
- Device: phones & tablets

# How to reproduce
1. Create a long list of favorites which is **longer** then the device screen
2. Scroll to the bottom of the list
3. Tap a session to launch the session details screen
4. Tap the device `BACK` button to return to the favorites list

# Observed behavior
- The list of favorites is shown. The list is scrolled to the **top**.

# Expected behavior
- The list of favorites is shown. The list is scrolled to the **bottom**.

# Hints
- Check if the behavior differs when the conference is in the future, present or past.

Changed files:
app/src/main/java/nerd/tuxmobil/fahrplan/congress/favorites/StarredListFragment.java

diff --git a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/favorites/StarredListFragment.java b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/favorites/StarredListFragment.java
index 42bea820..a204aae1 100644
--- a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/favorites/StarredListFragment.java
+++ b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/favorites/StarredListFragment.java
@@ -53,8 +53,11 @@ import static nerd.tuxmobil.fahrplan.congress.extensions.ViewExtensions.requireV
  * Activities containing this fragment MUST implement the {@link OnSessionListClick}
  * interface.
  */
-public class StarredListFragment extends AbstractListFragment implements AbsListView
-        .MultiChoiceModeListener {
+public class StarredListFragment extends AbstractListFragment implements
+        AbsListView.MultiChoiceModeListener,
+        AbsListView.OnScrollListener
+
+{
 
     private static final String LOG_TAG = "StarredListFragment";
     public static final String FRAGMENT_TAG = "starred";
@@ -75,6 +78,8 @@ public class StarredListFragment extends AbstractListFragment implements AbsList
      */
     private StarredListAdapter mAdapter;
 
+    private boolean preserveScrollPosition = false;
+
     public static StarredListFragment newInstance(boolean sidePane) {
         StarredListFragment fragment = new StarredListFragment();
         Bundle args = new Bundle();
@@ -100,6 +105,12 @@ public class StarredListFragment extends AbstractListFragment implements AbsList
             sidePane = args.getBoolean(BundleKeys.SIDEPANE);
         }
         setHasOptionsMenu(true);
+
+        Context context = requireContext();
+        starredList = appRepository.loadStarredSessions();
+        Meta meta = appRepository.readMeta();
+        mAdapter = new StarredListAdapter(context, starredList, meta.getNumDays());
+        MyApp.LogDebug(LOG_TAG, "initStarredList: " + starredList.size() + " favorites");
     }
 
     @Override
@@ -128,6 +139,8 @@ public class StarredListFragment extends AbstractListFragment implements AbsList
         mListView.setHeaderDividersEnabled(false);
         mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
         mListView.setMultiChoiceModeListener(this);
+        mListView.setAdapter(mAdapter);
+        mListView.setOnScrollListener(this);
         return view;
     }
 
@@ -136,17 +149,9 @@ public class StarredListFragment extends AbstractListFragment implements AbsList
     @Override
     public void onResume() {
         super.onResume();
-        initStarredList();
-        jumpOverPastSessions();
-    }
-
-    private void initStarredList() {
-        Context context = requireContext();
-        starredList = appRepository.loadStarredSessions();
-        Meta meta = appRepository.readMeta();
-        mAdapter = new StarredListAdapter(context, starredList, meta.getNumDays());
-        MyApp.LogDebug(LOG_TAG, "initStarredList: " + starredList.size() + " favorites");
-        mListView.setAdapter(mAdapter);
+        if (!preserveScrollPosition) {
+            jumpOverPastSessions();
+        }
     }
 
     private void jumpOverPastSessions() {
@@ -251,6 +256,18 @@ public class StarredListFragment extends AbstractListFragment implements AbsList
 
     }
 
+    @Override
+    public void onScrollStateChanged(AbsListView view, int scrollState) {
+        // Nothing to do here.
+    }
+
+    @Override
+    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
+        if (firstVisibleItem > 0) {
+            preserveScrollPosition = true;
+        }
+    }
+
     @Override
     public boolean onCreateActionMode(ActionMode mode, Menu menu) {
         return true;