【问题标题】:How to show alert dialog or dialog when press device back button or toolbar back arrow按下设备后退按钮或工具栏后退箭头时如何显示警报对话框或对话框
【发布时间】:2021-06-07 17:52:34
【问题描述】:

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

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

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

【问题讨论】:

  • 可以添加你的相关导航组件代码吗?

标签: java android kotlin


【解决方案1】:

在后退按钮上,您必须覆盖 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();
    }

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

【讨论】:

  • 是否有任何回调在导航组件中侦听后退箭头,以便我们可以应用此代码。 Bcoz 此代码适用于设备后退按钮,但如何监听导航组件工具栏后退箭头,因此我们执行此操作。
  • 对于后退箭头,您可以覆盖在活动中选择的选项项目,@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.YOURFRAGMENTNAME: // todo: 显示警报 return true;默认值:返回 super.onOptionsItemSelected(item); } }
  • 这不起作用,如果我使用菜单 xml 文件并通过工具栏上的菜单导航但我没有在我的项目中使用任何菜单文件我只是使用导航组件架构而不是任何菜单和没有任何抽屉。有没有其他方法可以帮助我们找到返回箭头列表
  • 最后我使用 binding.toolbar.setNavigationOnClickListener(this); 完成了导航组件工具栏的后退箭头;和 navController.navigateUp();
【解决方案2】:

最后我使用了导航组件工具栏后退箭头

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

这一行是导航的默认功能

 navController.navigateUp();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2011-10-30
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多