1

I want to keep scrollview scroll position of a fragment. This fragment (HomeFragment.kt) is one of several fragments within a fragment with bottom navigation (DashboardFragment.kt).

This is how HomeFragment.kt loaded (This is a snippet of DashboardFragment.kt)

    private fun loadHomeFragment() {

        homeFragment = HomeFragment()

        dashboardFragmentManager.beginTransaction()
            .setCustomAnimations(
                android.R.animator.fade_in,
                android.R.animator.fade_out
            )
            .replace(R.id.content, homeFragment, "1")
            .commit()
    }

And this is the scrollview layout (This is a snippet of HomeFragment.kt layout) :

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/swipeBaseLayout">

            <ScrollView
                android:id="@+id/baseLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingBottom="32dp">

                    <com.synnapps.carouselview.CarouselView
                        android:id="@+id/carouselView"
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        app:fillColor="#FFFFFFFF"
                        app:pageColor="#00000000"
                        app:pageTransformInterval="800"
                        app:radius="3dp"
                        app:slideInterval="5000"
                        app:strokeColor="#FFFFFFFF"
                        app:strokeWidth="1px" />


                    <RelativeLayout>
                       .....
                    </RelativeLayout>

                </RelativeLayout>
            </ScrollView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

As you can see, I already have an id for the scrollview. But every time this fragment loaded or navigated back from another fragment (From another fragment to DashboardFragment.kt) (I'm using navigation component), it'll start from the top.

Is there any way to keep scrollview even after the fragment transaction? If there's any detail I missed to point out, just let me know.

Krishna Sony
  • 1,286
  • 13
  • 27
dimas hermanto
  • 369
  • 7
  • 22
  • Since you are not adding transaction to back stack i think that's why it will not save the State. Is this the requirement without adding it to backstack ? – ADM Jan 05 '21 at 04:32
  • @ADM So... what should I use instead of `replace()` ? – dimas hermanto Jan 05 '21 at 04:35
  • I meant using [addTobackStack](https://stackoverflow.com/questions/22984950/what-is-the-meaning-of-addtobackstack-with-null-parameter) not changing `replace()`. – ADM Jan 05 '21 at 04:38
  • 1
    If you're using the Navigation Component, you shouldn't be using `beginTransaction()` at all. Which is it? Navigation Component or not? – ianhanniballake Jan 05 '21 at 04:52
  • Did you figure out what was the issue? @dimashermanto – Rohit Singh Oct 11 '21 at 18:44
  • My views are very complex, I have a nestedScrollView in a FragmentStateAdapter (main Fragment) which navigates to a secondary Fragment. For some reason your question @dimashermanto helped me with the sentence "I already have an id for the scrollview." I placed an ID and the problem went away. I am still concerned tho, I remember things where working a couple days ago, then I messed around with dataBinding dynamic visibility and I think this is what messed the saved state position. – Delark Sep 23 '22 at 23:02

1 Answers1

1

For more control on your backstack, you can have your custom stack and check there is only one instance of a fragment in the stack. Also for your problem, you can hide other fragments that are on top of the stack:

   showFragment(Fragment homeFragment) {
    fragmentTransaction = fragmentManager.beginTransaction();
    for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
        if (fragmentManager.getFragments().get(i) != homeFragment) {
            fragmentTransaction.hide(fragmentManager.getFragments().get(i));
        }
    }
    fragmentTransaction.show(homeFragment);
    fragmentTransaction.commit();
}
Amirhosein
  • 4,266
  • 4
  • 22
  • 35