片段上的导航视图句柄后退按钮 [英] Navigation view handle back button on fragment
本文介绍了片段上的导航视图句柄后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要处理片段上的后退按钮.我在移动应用程序上使用了导航.问题是打开新片段页面时如何处理后退按钮?
Hi I need to handle back button on fragment. I using this navigation on my mobile apps. The question is how to handle back button when I open new fragment page?
我尝试在新片段上使用以下代码.
I try using below code on new fragment.
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
但是,当单击后退"按钮时,导航将打开.有解决方案吗?
But when click the back button , the navigation will be open. Any solution ?
谢谢
推荐答案
您知道,每当用户按下Back
按钮时,您都需要转到先前加载的片段,然后尝试执行此操作(请注意,片段必须添加transaction.addToBackStack(null);
)
As you know whenever user presses Back
button you need to go to the previously loaded fragment and to do that try this (note fragment must be added with transaction.addToBackStack(null);
)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
int backStackCount = fragmentManager.getBackStackEntryCount();//check currently how many frags loaded
if (backStackCount > 0) {
fragmentManager.popBackStack(); //go back to previously loaded fragment
}
}
return super.onOptionsItemSelected(item);
}
这篇关于片段上的导航视图句柄后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文