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.

6
  • 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 ? Commented Jan 5, 2021 at 4:32
  • @ADM So... what should I use instead of replace() ? Commented Jan 5, 2021 at 4:35
  • I meant using addTobackStack not changing replace(). Commented Jan 5, 2021 at 4:38
  • 1
    If you're using the Navigation Component, you shouldn't be using beginTransaction() at all. Which is it? Navigation Component or not? Commented Jan 5, 2021 at 4:52
  • Did you figure out what was the issue? @dimashermanto Commented Oct 11, 2021 at 18:44

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.