之前,我在使用onBackPressed时遇到问题,并通过here帮助解决了该问题。

经过研究后,我发现由于我的起始片段位于后堆栈中,因此产生了额外的“后退”计数,因此当我尝试退出应用程序时,会出现空白屏幕:

java - onBackPressed和backStack退出前显示空白屏幕-LMLPHP
java - onBackPressed和backStack退出前显示空白屏幕-LMLPHP

而不是黑屏,我应该得到提示“您确定要退出”而不是黑屏。从空白屏幕上,我确实获得了“确定要退出”菜单,但是我不希望看到额外的空白屏幕。


  我还注意到,似乎并没有更新背面的片段标题,但是我认为这是另一个问题。


我不确定如何将我的家庭片段与backStack计数分开。如果我要避免将其添加到堆栈中,而是将其保留为导航项,则我对如何使用它感到困惑。

码:

public class Home extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private Fragment fragment = null;
private FragmentManager fragmentManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DatabaseHelper myDbHelper = new DatabaseHelper(Home.this);


    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setItemIconTintList(null);
//        getHome();

    displayView(R.id.nav_large_monsters);

}

private boolean viewIsAtHome;

@Override
public void onBackPressed() {
    // Pressing back popped the back stack, nothing else to do
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (!fragmentManager.isStateSaved() && fragmentManager.popBackStackImmediate()) {
        return;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        }

    if (!viewIsAtHome) {
            displayView(R.id.nav_large_monsters);
        } else {
            new AlertDialog.Builder(this)
                    .setMessage("Are you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Home.this.finish();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();
     }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}


private void getHome(){
    fragment = new HomeFragment();
    if (fragment != null) {
        FragmentManager fragmentMgmt = getSupportFragmentManager();
        fragmentMgmt.beginTransaction().replace(R.id.content_frame, fragment).commit();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    displayView(item.getItemId());
    return true;

}

public void displayView(int viewId) {

//        Fragment fragment = null;
    String title = getString(R.string.app_name);

    switch (viewId) {
        default:
            fragment = new Large_Monsters();
            title  = "Large Monsters";
            break;

        case R.id.nav_large_monsters:
            fragment = new Large_Monsters();
            title  = "Large Monsters";
            viewIsAtHome = true;
            break;

        case R.id.nav_small_monsters:
            fragment = new Small_Monsters();
            title = "Small Monsters";
            viewIsAtHome = false;
            break;

        case R.id.nav_weapons:
            fragment = new Weapons();
            title = "Weapons";
            viewIsAtHome = false;
            break;

        case R.id.nav_armors:
            fragment = new Armors_Low_High();
            title = "Armor Sets";
            viewIsAtHome = false;
            break;

        case R.id.nav_charms:
            fragment = new Charms();
            title = "Charms";
            viewIsAtHome = false;
            break;

        case R.id.nav_items:
            fragment = new Items();
            title = "Items";
            viewIsAtHome = false;
            break;

        case R.id.palico_armor:
            fragment = new Palico_Armor();
            title = "Palico Armor";
            viewIsAtHome = false;
            break;

        case R.id.palico_gadgets:
            fragment = new Palico_Gadgets();
            title = "Palico Gadgets";
            viewIsAtHome = false;
            break;

        case R.id.palico_weps:
            fragment = new Palico_Weapons();
            title = "Palico Weapons";
            viewIsAtHome = false;
            break;

        case R.id.palico_helms:
            fragment = new Palico_Helms();
            title = "Palico Helmets";
            viewIsAtHome = false;
            break;

    }

    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.content_frame, fragment, fragment.getTag());
                ft.addToBackStack(null);
                ft.commit();
    }

    // set the toolbar title
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(title);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

}

}

最佳答案

您只需要在首页上调用addToBackStack(null)(例如:Large_Monsters片段)

所以只要像这样添加onCreate方法

Large_Monsters fragment = new Large_Monsters();

if (fragment != null) {
    FragmentManager fragmentMgmt = getSupportFragmentManager();
    fragmentMgmt.beginTransaction().replace(R.id.content_frame, fragment).commit();
}


在左侧菜单中,您只需要检查此片段是否存在,如果不存在,则将其替换,否则请删除其他片段。

07-26 02:26