【发布时间】: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);
这适用于设备后退按钮,但不适用于导航组件工具栏后退箭头。
【问题讨论】:
-
可以添加你的相关导航组件代码吗?