我已将以下代码添加到我的基本活动中,以在我的应用中实现后退功能
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
System.out.println("============This Is Back Pressed==============");
System.out.println("===============getBackStackEntryCount()==========="+getSupportFragmentManager().getBackStackEntryCount());
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
{
this.finish();
return false;
}
else
{
getSupportFragmentManager().popBackStack();
return false;
}
}
for (MyonBackPressed listener : onBackPressedListeners)
{
listener.Onback(keyCode,event);
}
System.out.println("====>"+onBackPressedListeners);
return super.onKeyDown(keyCode, event);
}
但按下后退按钮时出现此错误 我在may应用程序中使用滑动菜单库
03-27 14:30:15.835: I/System.out(7118): ============This Is Back Pressed==============
03-27 14:30:15.835: I/System.out(7118): ===============getBackStackEntryCount()===========1
03-27 14:30:15.855: I/System.out(7118): i===>1
03-27 14:30:15.875: I/System.out(7118): all event url http://54.200.110.49/checkplanner/webservice/alleventlist.php?email=test.prismetric@gmail.com&limit=0
03-27 14:30:15.875: W/dalvikvm(7118): threadid=1: thread exiting with uncaught exception (group=0x40bab450)
03-27 14:30:15.935: E/AndroidRuntime(7118): FATAL EXCEPTION: main
03-27 14:30:15.935: E/AndroidRuntime(7118): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.view.ViewGroup.addViewInner(ViewGroup.java:3486)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.view.ViewGroup.addView(ViewGroup.java:3357)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.view.ViewGroup.addView(ViewGroup.java:3302)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.view.ViewGroup.addView(ViewGroup.java:3278)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
0 3-27 14:30:15.935: E/AndroidRuntime(7118): at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1516)
03-27 14:30:15.935: E/AndroidRuntime(7118): at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:479)
答案 0 :(得分:0)
当您在当时向活动添加片段时
getSupportFragmentManager()
.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack("perent") // this is store fragment in to stack and you can get back to previous fragment
.add(R.id.fragment_container, fragmentObj)
.commit();