https://play.google.com/store/apps/details?id=com.ipllive2018.streaming
在应用程序中,当我点击菜单项时,它将加载相应的项目,但当我点击它退出它退出而不进入主屏幕。我想帮助重新开始修复它。
第二我还想添加退出按钮,例如"你想退出"像那样
我真的很抱歉。我搜索并尝试了很多代码。我绝对是一个新手,它限制了我的行为。我正在粘贴我的应用程序源文件
@Override
public void onBackPressed() {
Fragment activeFragment = null;
if (adapter != null)
activeFragment = adapter.getCurrentFragment();
if (drawer != null && drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (activeFragment instanceof BackPressFragment) {
boolean handled = ((BackPressFragment) activeFragment).handleBackPress();
if (!handled) {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}

答案 0 :(得分:0)
由于您使用的是片段,我建议使用fragmentTransaction的addToBackStack()。在HomeFragment中,执行此操作
ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new otherFragment).addToBackStack(null).commit();
然后按下,检查FragmentManager的堆栈计数
if (fm.getBackStackEntryCount() == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit app?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.super.onBackPressed();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
} else {
fm.popBackStackImmediate();
}