[英]How to disable the back button press in fragment but allow the fragment itself to implement the back pressing logics
I have the following XML -我有以下 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)看起来像这样 - (这不是图片中显示的整个 XML,而只是与问题相关的那个)
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.它的工作方式是,对于您在 XML 中看到的每个 FrameLayout,我会在其中依次为每个阶段填充另一个 Fragment。
Inside each fragment, when I need to dismiss that fragment from the UI, I call the following code -在每个片段中,当我需要从 UI 中消除该片段时,我调用以下代码 -
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.我目前面临的问题是能够通过一次后按来关闭片段,这对 UI 不利。 I could easily dismiss the onBackPressed()
ability, but than how would I be able to tell each fragment to dismiss itself?我可以很容易地解除onBackPressed()
能力,但是我怎么能告诉每个片段解除自己呢?
I need a way to block the fragment from reacting to backpress but still have the ability to remove itself from the backstack.我需要一种方法来阻止片段对 backpress 做出反应,但仍然能够将自己从 backstack 中移除。
每当您想关闭 Fragment 时,请调用getFragmentManager().popBackStackImmediate()
而不是onBackPressed()
。
使用 BackStack 和 push ,弹出片段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.