0

I have the following XML -

<androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activity_checkout_top_view">

        <LinearLayout
            android:id="@+id/activity_checkout_checkout_stages_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="100dp"
            android:animateLayoutChanges="true"
            android:background="@color/white"
            android:orientation="vertical">

            <FrameLayout
                android:id="@+id/activity_checkout_discount_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:text="@string/activity_checkout_checkout"
                android:textSize="22sp"
                android:textStyle="bold" />

            <LinearLayout
                android:id="@+id/activity_checkout_first_stage_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/activity_checkout_first_stage_textview"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginTop="@dimen/marketplace_15dp"
                    android:background="@color/color_black"
                    android:gravity="center"
                    android:text="@string/activity_checkout_1"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/activity_checkout_billing_address_textview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:text="@string/activity_checkout_billing_address"
                    android:textColor="@color/color_black"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <View
                android:id="@+id/billing_shipping__address_layout_bottom_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:background="@color/activity_checkout_stages_line" />


            <!-- Billing layout -->
            <include
                android:id="@+id/checkout_activity_billing_address_layout_include"
                layout="@layout/fragment_checkout_billing_shipping"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/activity_checkout_billing_address_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:id="@+id/activity_checkout_second_stage_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/activity_checkout_second_stage_textview"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginTop="@dimen/marketplace_15dp"
                    android:background="@color/activity_checkout_unselected_checkout_stage_color"
                    android:gravity="center"
                    android:text="@string/activity_checkout_2"
                    android:textColor="@color/color_black" />

                <TextView
                    android:id="@+id/activity_checkout_shipping_address_textview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:text="@string/activity_checkout_shipping_address"
                    android:textColor="@color/activity_checkout_unselected_checkout_stage_color"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <View
                android:id="@+id/activity_checkout_shipping_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:background="@color/activity_checkout_stages_line" />

            <!-- Shipping layout -->
            <include
                android:id="@+id/activity_checkout_shipping_address_layout_include"
                layout="@layout/fragment_checkout_billing_shipping"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/activity_checkout_shipping_address_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:id="@+id/activity_checkout_third_stage_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/activity_checkout_third_stage_textview"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginTop="@dimen/marketplace_15dp"
                    android:background="@color/activity_checkout_unselected_checkout_stage_color"
                    android:gravity="center"
                    android:text="@string/activity_checkout_3"
                    android:textColor="@color/color_black" />

                <TextView
                    android:id="@+id/activity_checkout_shipping_method_textview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:text="@string/activity_checkout_shipping_method"
                    android:textColor="@color/activity_checkout_unselected_checkout_stage_color"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <View
                android:id="@+id/activity_checkout_payment_method_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:background="@color/activity_checkout_stages_line" />

            <FrameLayout
                android:id="@+id/activity_checkout_shipping_method_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:id="@+id/activity_checkout_forth_stage_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginTop="@dimen/marketplace_15dp"
                    android:background="@color/activity_checkout_unselected_checkout_stage_color"
                    android:gravity="center"
                    android:text="@string/activity_checkout_4"
                    android:textColor="@color/color_black" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:text="@string/activity_checkout_payment_method"
                    android:textColor="@color/activity_checkout_unselected_checkout_stage_color"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <View
                android:id="@+id/activity_checkout_payment_information_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:background="@color/activity_checkout_stages_line" />

            <LinearLayout
                android:id="@+id/activity_checkout_fifth_stage_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/activity_checkout_fifth_stage"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginTop="@dimen/marketplace_15dp"
                    android:background="@color/activity_checkout_unselected_checkout_stage_color"
                    android:gravity="center"
                    android:text="@string/activity_checkout_5"
                    android:textColor="@color/color_black" />

                <TextView
                    android:id="@+id/activity_checkout_payment_information"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:text="@string/activity_checkout_payment_information"
                    android:textColor="@color/activity_checkout_unselected_checkout_stage_color"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>


            <View
                android:id="@+id/activity_checkout_confirm_order_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:background="@color/activity_checkout_stages_line" />

            <FrameLayout
                android:id="@+id/activity_checkout_payment_information_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:id="@+id/activity_checkout_sixth_stage_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/activity_checkout_sixth_stage"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginTop="@dimen/marketplace_15dp"
                    android:background="@color/activity_checkout_unselected_checkout_stage_color"
                    android:gravity="center"
                    android:text="@string/activity_checkout__6"
                    android:textColor="@color/color_black" />

                <TextView
                    android:id="@+id/activity_checkout_confirm_information_textview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:text="@string/activity_checkout_confirm_information"
                    android:textColor="@color/activity_checkout_unselected_checkout_stage_color"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <View
                android:id="@+id/activity_checkout_confirm_information_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:background="@color/activity_checkout_stages_line" />

            <FrameLayout
                android:id="@+id/activity_checkout_confirm_information_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


        </LinearLayout>


    </androidx.core.widget.NestedScrollView>

Which looks like this - (This is not the entire XML as shown in the picture, but only the one relevant to the question)

enter image description here

The way it works is that for every FrameLayout you see in the XML I inflate another Fragment inside it, in turns for each stage respectively.

Inside each fragment, when I need to dismiss that fragment from the UI, I call the following code -

activity?.onBackPressed()

Which dismisses the relevant Fragment and after that I pop a new one.

The problem I am currently facing is the ability to close the fragment with a single back press, which is bad for the UI. I could easily dismiss the onBackPressed() ability, but than how would I be able to tell each fragment to dismiss itself?

I need a way to block the fragment from reacting to backpress but still have the ability to remove itself from the backstack.

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46

2 Answers2

1

Whenever you want to dismiss a Fragment, call getFragmentManager().popBackStackImmediate() instead of onBackPressed().

Shahood ul Hassan
  • 745
  • 2
  • 9
  • 19
-2

Use BackStack and push , pop fragment

G Mehdi Balti
  • 154
  • 2
  • 12