早些时候,我遇到了onBackPressed的问题,并通过一些帮助here解决了问题。
在做了一些研究后,我发现由于我的起始片段在后台堆叠,它产生了一个额外的'返回'算一下,当我试图退出我的应用程序时,我得到一个空白屏幕:
而不是空白屏幕我应该得到提示"你确定退出"而不是空白。从空白的屏幕上,我确实得到了你的确定退出'菜单,但我不希望看到一个额外的空白屏幕。
我还注意到它似乎没有更新背面的片段标题,但我假设这是另一个问题。
我不确定如何将我的主片段与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);
}
}
答案 0 :(得分:1)
我想我在发布此内容之后就明白了......
onBackPressed()中的部分:
if (!viewIsAtHome) {
displayView(R.id.nav_large_monsters);
}
是否有帮助设置检查主页的内容,但如果我使用我的getHome()方法,并在OnCreate下设置:
private void getHome(){
fragment = new Large_Monsters();
if (fragment != null) {
FragmentManager fragmentMgmt = getSupportFragmentManager();
fragmentMgmt.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
}
这是让我设置我的主页,并避免在onBackPressed上进行双重检查。但主要是,这避免了在我的onNavigationItemSelected区域设置检查,让我们像往常一样处理onBackPressed。
答案 1 :(得分:1)
您不必在主页上调用 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();
}
在你的左侧菜单中,你只需要检查这个片段是否存在,如果不存在则更换它,否则删除其他片段。