2

I am using navigation components in my Android Project.I am using One Activity model and following this model I have 1 Base Activity and the rest are all Fragments.
My problem comes on pressing the back button when i have to move to back to a previous fragment.I have Scroll view in my fragment named WorkerAnalyticsFragment shown below.

enter image description here

The Container shown in the center contains the Scroll view as the root view to scroll the contents inside container when Order analytics and Motivation analytics are expand to show details.The xml code of the fragment_worker_analytics is shown below.

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".Fragment.WorkerAnalyticsFragment"
    android:background="@color/uniform_background">

    <LinearLayout
        android:id="@+id/profile_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/profile_container_margin_top">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/circle_image_view_large"
            android:layout_width="@dimen/circle_image_view_large_width"
            android:layout_height="@dimen/circle_image_view_large_height"
            android:src="@drawable/example_avatar"
            android:layout_gravity="center_horizontal"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_view_profile_name"
            android:maxLines="1"
            android:layout_gravity="center_horizontal"
            style="@style/UniformTextAppearance"/>

    </LinearLayout>

    <include layout="@layout/analytics_container" android:id="@+id/analytics_container"/>

    <include layout="@layout/order_and_report_buttons" android:id="@+id/order_and_report_buttons"/>

</LinearLayout>

</layout>

The xml code of the analytics_container which is shown in the center in the above picture is here.

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:layout_margin="@dimen/analytics_container_margin"
    android:background="@drawable/container_background"
    android:minHeight="350dp"
    android:scrollbars="none"
    android:padding="@dimen/analytics_container_padding">

    <LinearLayout
        android:id="@+id/analytics_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/current_order_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_view_current_order_status_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_view_current_order_status_text"
                android:textStyle="bold"
                style="@style/UniformTextAppearance"/>

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center">

                <LinearLayout
                    android:id="@+id/current_order_details_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_marginTop="@dimen/step_view_margin_top"
                    android:visibility="visible">

                    <com.kofigyan.stateprogressbar.StateProgressBar
                        android:id="@+id/order_progress_bar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:spb_maxStateNumber="four"
                        app:spb_stateDescriptionSize="@dimen/sbp_state_description_text_size"
                        app:spb_maxDescriptionLines="1"
                        app:spb_stateBackgroundColor="@color/spb_state_background_color"
                        app:spb_stateForegroundColor="@color/spb_state_foreground_color"
                        app:spb_descriptionTopSpaceIncrementer="@dimen/spb_description_top_Space_incrementer"
                        app:spb_currentStateDescriptionColor="@color/spb_current_state_description_color"
                        app:spb_stateDescriptionColor="@color/spb_state_description_color"
                        app:spb_stateNumberBackgroundColor="@color/spb_state_number_background_color"/>

                    <include layout="@layout/current_item" android:id="@+id/current_item"/>

                </LinearLayout>

                <TextView
                    android:id="@+id/empty_text_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/no_current_order_text"
                    android:textSize="@dimen/empty_text_size"
                    android:textStyle="bold"
                    android:layout_gravity="center"
                    android:visibility="invisible"
                    android:background="@color/current_item_container_background_color"
                    style="@style/EmptyTextAppearance"
                    />

            </FrameLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/profile_analytics_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="@dimen/profile_container_margin_top">

            <TextView
                android:id="@+id/text_view_worker_analytics"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_view_worker_analytics_text"
                android:textStyle="bold"
                style="@style/UniformTextAppearance"/>

            <include layout="@layout/card_view_analytics_container"/>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

</layout>

I have another fragment named OrderFragment which comes out when the user clicks ORDER button as shown in above picture.Here is the picture.

enter image description here

The code of the OrderFragment is shown below.

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
    android:id="@+id/stock_fragment_parent_layout_padding"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/stock_fragment_parent_layout_padding"
    android:background="@color/uniform_background"
    tools:context=".Fragment.OrderFragment">

    <LinearLayout
        android:id="@+id/stock_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginTop="@dimen/stock_container_margin_top"
        android:background="@drawable/container_background">

        <TextView
            android:id="@+id/text_view_available_stock_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/text_view_available_stock_text"
            style="@style/UniformTextAppearance"/>

        <LinearLayout
            android:id="@+id/stock_list_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/stock_list_container_margin_top">

            <include layout="@layout/recycler_view" android:id="@+id/recycler_view"/>

        </LinearLayout>

    </LinearLayout>



</LinearLayout>

</layout>

The problem comes when i press the Back button to return to the previous WorkerAnayticsFragment shown above by xml code and by picture, my application crashes.
Here is my Log cat showing the error when application crash.

2020-03-27 01:26:57.757 20708-20708/com.example.nibotransporti E/AndroidRuntime: FATAL EXCEPTION: 
 main
Process: com.example.nibotransporti, PID: 20708
java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to 
    android.widget.ScrollView$SavedState
    at android.widget.ScrollView.onRestoreInstanceState(ScrollView.java:1872)
    at android.view.View.dispatchRestoreInstanceState(View.java:17819)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3773)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3781)
    at android.view.View.restoreHierarchyState(View.java:17797)
    at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:574)
    at androidx.fragment.app.FragmentStateManager.restoreViewState(FragmentStateManager.java:505)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1164)
    at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2209)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1976)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1915)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1811)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:427)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
    2020-03-27 01:26:57.799 20708-20708/com.example.nibotransporti I/Process: Sending signal. PID: 
    20708 SIG: 9

I am in a serious trouble.Please help me by suggesting me a robust solution.

Mr U9Q
  • 111
  • 6

1 Answers1

0

your code is not complete,but I guess you used the same id for view.

when this fragment rebuild,these same id might lead to the problem.

you can try to change the id of scrollView,should be solved