当我按下后退按钮,它是移动到上一个片段,但也显示一个空白屏幕,我需要点击它来消除它。我该如何摆脱这个空白屏幕?我看到它和第一个有某种联系 addToBackStack
但我还是修不好
这是我的 onNavigationItemSelected
方法:
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment=null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
fragment=new HomeFragment();
break;
case R.id.nav_income:
fragment=new IncomeFragment();
break;
case R.id.nav_expense:
fragment=new ExpenseFragment();
break;
case R.id.nav_sign_out:
FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.signOut();
startActivity(new Intent(getApplicationContext(), WelcomeActivity.class));
Toast.makeText(getApplicationContext(), "Signed Out", Toast.LENGTH_LONG).show();
break;
}
if(fragment!=null)
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container, fragment).addToBackStack("").commit();
return true;
}
这是我的 onBackPressed()
方法:
public void onBackPressed() {
final FragmentManager fragmentManager = getSupportFragmentManager();
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
if (fragmentManager.getBackStackEntryCount() == 0) {
builder.setTitle("Sign out");
builder.setMessage("Are you sure you want to sign out?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
firebaseAuth.signOut();
dialog.dismiss();
startActivity(new Intent(getApplicationContext(), WelcomeActivity.class));
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
} else {
fragmentManager.popBackStack();
}
AlertDialog alert = builder.create();
alert.show();
}
}
3条答案
按热度按时间6mzjoqzu1#
你需要改变,如果条件如下
根据您的代码,请找到下面的完整代码
我希望这能帮助你!
谢谢您。
628mspwn2#
更新所有片段的oncreateview
希望这会有帮助!!
ygya80vv3#
嗨,我猜这是因为最后一个片段被删除,你看到的是一个空白的屏幕,这是你添加片段的片段容器。因此,当到达最后一个屏幕时,请在开始另一个屏幕之前完成该活动。使用此代码启动新活动。