简体   繁体   English

按下设备后退按钮或工具栏后退箭头时如何显示警报对话框或对话框

[英]How to show alert dialog or dialog when press device back button or toolbar back arrow

I am using navigation component, and when I use device back button or toolbar back arrow I want to show alert dialog or custom dialog before navigate to previous fragment, if user press yes then navigate to the previous fragment, anything else dismiss like a dialog functionality.我正在使用导航组件,当我使用设备后退按钮或工具栏后退箭头时,我想在导航到上一个片段之前显示警报对话框或自定义对话框,如果用户按下是,则导航到上一个片段,其他任何东西都像对话框功能一样关闭. I already tried this:我已经尝试过了:

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);

This worked for the device back button but not for the navigation component toolbar back arrow.这适用于设备后退按钮,但不适用于导航组件工具栏后退箭头。

on back button you have to override the onbackpress method and inside it you would write your custom alert dialog something like this:在后退按钮上,您必须覆盖 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();
    }

and for back arrow same concept I would suggest to write a method for back arrow and back button contains the alert and call it when these button pressed对于后退箭头相同的概念,我建议为后退箭头编写一个方法,并且后退按钮包含警报并在按下这些按钮时调用它

finally i have done the navigation component toolbar back arrow using最后我完成了导航组件工具栏后退箭头使用

 binding.toolbar.setNavigationOnClickListener(this);

  @Override
    public void onClick(View v) {
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
        Fragment fragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
        if (fragment instanceof HostFragment) {
            if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
                binding.drawerLayout.closeDrawer(GravityCompat.START, true);
            } else {
                binding.drawerLayout.openDrawer(GravityCompat.START, true);
            }
        } else {
            navController.navigateUp();
        }
    }

this line make the default functionality of the navigation此行使导航的默认功能

 navController.navigateUp();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM