首页 > 解决方案 > 按下设备后退按钮或工具栏后退箭头时如何显示警报对话框或对话框

问题描述

我正在使用导航组件,当我使用设备后退按钮或工具栏后退箭头时,我想在导航到上一个片段之前显示警报对话框或自定义对话框,如果用户按下是,则导航到上一个片段,其他任何东西都像对话框功能一样关闭. 我已经尝试过了:

OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {

    @Override
    public void handleOnBackPressed() {
        // Handle the back button event
    }
};

this is my code for fragment and actions and i asked to how to handle the backarrow event or callback in navigation component architecture so as i move to previous fragment so before going to back it will display a alert are you sure to move from here like that.

  <fragment
        android:id="@+id/drawOnImageFragment"
        android:name="com.consulthealthcare.app.fragments.DrawOnImageFragment"
        android:label="Mark on Prescription"
        tools:layout="@layout/fragment_draw_on_image">
        <argument
            android:name="uri"
            app:argType="string" />
        <action
            android:id="@+id/action_drawOnImageFragment_to_confirmOrderFragment"
            app:destination="@id/confirmOrderFragment"
            app:enterAnim="@anim/enter_from_right"
            app:exitAnim="@anim/exit_to_left"
            app:popEnterAnim="@anim/enter_from_left"
            app:popExitAnim="@anim/exit_to_right" />
        <action
            android:id="@+id/action_drawOnImageFragment_to_dialogNavFragment"
            app:destination="@id/dialogNavFragment" />
    </fragment>

requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

这适用于设备后退按钮,但不适用于导航组件工具栏后退箭头。

标签: javaandroidkotlin

解决方案


在后退按钮上,您必须覆盖 onbackpress 方法,并在其中编写自定义警报对话框,如下所示:

 @Override
    public void onBackPressed()
    {
  
        // Create the object of
        // AlertDialog Builder class
        AlertDialog.Builder builder
            = new AlertDialog
                  .Builder(context/* requireActivity() in fragment*/ );
  
        // Set the message show for the Alert time
        builder.setMessage("Do you want to go back ?");
  
        // Set Alert Title
        builder.setTitle("Alert !");
  
        // Set Cancelable false
        // for when the user clicks on the outside
        // the Dialog Box then it will remain show
        builder.setCancelable(false);
  
        // Set the positive button with yes name
        // OnClickListener method is use of
        // DialogInterface interface.
  
        builder
            .setPositiveButton(
                "Yes",
                new DialogInterface
                    .OnClickListener() {
  
                        @Override
                        public void onClick(DialogInterface dialog,
                                            int which)
                        {
  
                            // write what you want to do here for yes
                        }
                    });
  
        // Set the Negative button with No name
        // OnClickListener method is use
        // of DialogInterface interface.
        builder
            .setNegativeButton(
                "No",
                new DialogInterface
                    .OnClickListener() {
  
                        @Override
                        public void onClick(DialogInterface dialog,
                                            int which)
                        {
  
                            // If user click no
                            // then dialog box is canceled.
                            dialog.cancel();
                        }
                    });
  
        // Create the Alert dialog
        AlertDialog alertDialog = builder.create();
  
        // Show the Alert Dialog box
        alertDialog.show();
    }

对于后退箭头相同的概念,我建议为后退箭头编写一个方法,并且后退按钮包含警报并在按下这些按钮时调用它


推荐阅读