按后退按钮移动到上一个片段时显示空白屏幕

g9icjywg  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(322)

当我按下后退按钮,它是移动到上一个片段,但也显示一个空白屏幕,我需要点击它来消除它。我该如何摆脱这个空白屏幕?我看到它和第一个有某种联系 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();
        }
    }
6mzjoqzu

6mzjoqzu1#

你需要改变,如果条件如下

if(fragmentManager.getBackStackEntryCount() - 1 == 0){
    //Do your code
}else{
    fragmentManager.popBackStack();
}

根据您的代码,请找到下面的完整代码

public void onBackPressed() {
    final FragmentManager fragmentManager = getSupportFragmentManager();
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        if (fragmentManager.getBackStackEntryCount() - 1 == 0) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
            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();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else {
            fragmentManager.popBackStack();
        }
    }
}

我希望这能帮助你!
谢谢您。

628mspwn

628mspwn2#

更新所有片段的oncreateview

View view; 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    if (view != null) {
    if (view.getParent() != null) {
        ((ViewGroup)view.getParent()).removeView(view);
    }
    return view;
}

View v=inflater.inflate(R.layout.tests, container, false);

希望这会有帮助!!

ygya80vv

ygya80vv3#

嗨,我猜这是因为最后一个片段被删除,你看到的是一个空白的屏幕,这是你添加片段的片段容器。因此,当到达最后一个屏幕时,请在开始另一个屏幕之前完成该活动。使用此代码启动新活动。

Intent intent=new Intent(getApplicationContext(), WelcomeActivity.class);
finish();   //finish used to finish blank screen activity
startActivity(intent);

相关问题