Android addToBackStack关闭应用程序

时间:2015-02-05 13:38:56

标签: android android-activity android-fragments back-stack

在我的应用中,我正在使用显示不同片段的活动。此活动还有一个抽屉布局。

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");
    }
}

我的应用程序工作正常,但当我显示第二个或第三个片段,然后按手机上的后退按钮时,应用程序就会关闭。我不知道为什么addToBackStack(null)不起作用?

完整Logcat: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)

2 个答案:

答案 0 :(得分:1)

尝试

transaction.addToBackStack("saveState");

而不是null尝试任何String值并检查。如果它不起作用,请粘贴整个LogCat。

编辑:根据您最新的LogCat更新,我认为您已将this作为Context传递。您需要将getActivity()代替'this'作为context

答案 1 :(得分:0)

你必须先清除后面的堆栈!

if (fragment != null) {
            FragmentManager fm = getSupportFragmentManager();

            //clear the back stack when click happens 
            while (fm.getBackStackEntryCount() > 1) {
                fm.popBackStackImmediate();
            }

            FragmentTransaction ft = fm.beginTransaction();
            ft.addToBackStack(null);
            ft.replace(R.id.frame_container, fragment).commit();

            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);


    enter code here