내 애플 리케이션에서 다른 조각을 표시 활동과 함께 일하고 있어요. 또한 액티비티에는 drawerlayout이 있습니다.안드로이드 addToBackStack 애플 리케이션을 닫습니다
activity_main.xml :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>
그래서 지금은 그들을 대체하여 조각을 보여주기 위해 노력하고있어. 이것은 잘 작동합니다! 그러나 조각을 백 스택에 추가하고 싶습니다.
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new firstFragment();
break;
case 1:
fragment = new secondFragment();
break;
default:
break;
}
if (fragment != null) {
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
내 응용 프로그램이 잘 작동하지만 내가 두 번째 또는 세 번째 조각을 표시 한 후 내 휴대 전화의 뒤로 가기 (back) 버튼을 누를 때, 응용 프로그램은 종료 : 그래서 이렇게. 왜 addToBackStack (null)이 작동하지 않는지 모르겠다.
전체 로그 캣 : http://pastebin.com/8NPHgqXi
관련 부분 :
02-05 15:50:08.002 7844-7844/example.de.example D/ViewGroup﹕ addInArray been called, this = android.support.v7.widget.ActionMenuView{421451d8 V.E..... ......ID 540,0-540,84}call stack =
java.lang.Throwable: addInArray
at android.view.ViewGroup.addInArray(ViewGroup.java:3786)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3740)
at android.view.ViewGroup.addView(ViewGroup.java:3564)
at android.view.ViewGroup.addView(ViewGroup.java:3509)
at android.support.v7.internal.view.menu.BaseMenuPresenter.addItemView(BaseMenuPresenter.java:133)
at android.support.v7.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:107)
at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:207)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:279)
at android.support.v7.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1021)
at android.support.v7.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:1044)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1021)
at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:115)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
내 실수로 인해서 내 활동에서 onBackPressed()를 재정의하지 않았습니다. 나는이 솔루션을 따라 갔다. http://stackoverflow.com/questions/23728216/back-button-closing-app-even-when-using-fragmenttransaction-addtobackstack – RyuZz