问题描述
我想知道如何使用导航控制器正确处理系统后退按钮操作.在我的应用程序中,我有两个片段(例如片段 1 和片段 2),并且我在片段 1 中有一个动作,目标是片段 2.除了一件事之外,一切都运行良好 - 当用户按下 fragment2 中的系统后退按钮时,我想显示一个对话框(例如使用 DialogFragment)以确认退出.实现这种行为的最佳方法是什么?如果我在我的主机片段中使用 app:defaultNavHost="true"
那么它会自动返回忽略我的规则.另外,这个组件有什么用?
I'd like to know how properly handle system back button action using Navigation Controller. In my app I have two fragments (for ex. fragment1 and fragment2) and I have an action in fragment1 with destination to fragment2. Everything works well except one thing - when user presses system back button in fragment2 I want to show a dialog (using DialogFragment for example) to confirm exit. What is the best way to implement this behavior? If I use app:defaultNavHost="true"
in my host fragment then it automatically goes back ignoring my rules. And, additionally, what is this component for?
我应该使用pop to"吗?
Should I use "pop to" may be?
推荐答案
最新更新 - 2019 年 4 月 25 日
新版本 androidx.activity 版本.1.0.0-alpha07带来了一些变化
android官方指南中有更多解释:提供自定义返回导航
More explanations in android official guide: Provide custom back navigation
示例:
public class MyFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This callback will only be called when MyFragment is at least Started.
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
@Override
public void handleOnBackPressed() {
// Handle the back button event
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
// The callback can be enabled or disabled here or in handleOnBackPressed()
}
...
}
旧更新
更新日期:2019 年 4 月 3 日
现在简化了.更多信息此处
示例:
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), this);
@Override
public boolean handleOnBackPressed() {
//Do your job here
//use next line if you just need navigate up
//NavHostFragment.findNavController(this).navigateUp();
//Log.e(getClass().getSimpleName(), "handleOnBackPressed");
return true;
}
已弃用(自版本 1.0.0-alpha062019 年 4 月 3 日):
Deprecated (since Version 1.0.0-alpha06April 3rd, 2019) :
由于这个,它可以只实现在片段中使用 JetPack 实现 OnBackPressedCallback
并将其添加到活动中:getActivity().addOnBackPressedCallback(getViewLifecycleOwner(),this);
Since this, it can be implemented just using JetPack implementation OnBackPressedCallback
in your fragmentand add it to activity:getActivity().addOnBackPressedCallback(getViewLifecycleOwner(),this);
您的片段应如下所示:
public MyFragment extends Fragment implements OnBackPressedCallback {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().addOnBackPressedCallback(getViewLifecycleOwner(),this);
}
@Override
public boolean handleOnBackPressed() {
//Do your job here
//use next line if you just need navigate up
//NavHostFragment.findNavController(this).navigateUp();
//Log.e(getClass().getSimpleName(), "handleOnBackPressed");
return true;
}
@Override
public void onDestroyView() {
super.onDestroyView();
getActivity().removeOnBackPressedCallback(this);
}
}
UPD:您的 Activity 应该扩展 AppCompatActivity
或 FragmentActivity
并在 Gradle 文件中:
UPD:Your activity should extends AppCompatActivity
or FragmentActivity
and in Gradle file:
implementation 'androidx.appcompat:appcompat:{lastVersion}'
这篇关于处理 Android 导航组件中的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!