我想onBackPressed在片段中处理我在我的活动中使用以下代码,但这返回 0@Override public void onBackPressed() { int count = getFragmentManager().getBackStackEntryCount(); Toast.makeText(getBaseContext(), ""+count, Toast.LENGTH_SHORT).show(); if (count == 1) { super.onBackPressed(); //additional code } else { getFragmentManager().popBackStack(); }}
2 回答
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
只需添加.addToBackStack(null)
您的Activity
or Fragment
。当您添加或替换fragment
. 像下面
getSupportFragmentManager().beginTransaction().replace(R.id.parent_layout, new MyFragment()).addToBackStack(null).commit();
注意:-您不必在onBackPressed()
方法中做任何事情。
慕标琳琳
TA贡献1830条经验 获得超9个赞
Just app this line when you want to replace any fragment and manage backsack on Fragment.ClientHome per = new ClientHome(); Bundle bundle = new Bundle(); bundle.putString("usertype", "client"); per.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.slide_from_left, R.anim.slide_to_right); fragmentTransaction.replace(R.id.content_frame, per, "tag"); fragmentTransaction.addToBackStack("tag"); fragmentTransaction.commit();
添加回答
举报
0/150
提交
取消