2

in my app I'm working with an activity which displays different Fragments. Also the activity has a drawerlayout.

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>

So now I'm trying to show the fragments by replacing them. This works fine! But also I want to add the fragments to the backstack. So I do this:

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

My app works fine, but when I display the second or third fragment and then press the back button on my phone, the app just closes. I don't know why addToBackStack(null) doesn't work?

Full Logcat : http://pastebin.com/8NPHgqXi

Relevant part :

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)
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
RyuZz
  • 581
  • 8
  • 30
  • Just found my mistake, I doesn't override the onBackPressed() in my activity. I followed this solution http://stackoverflow.com/questions/23728216/back-button-closing-app-even-when-using-fragmenttransaction-addtobackstack – RyuZz Feb 05 '15 at 15:21

2 Answers2

1

Try

transaction.addToBackStack("saveState");

instead of null try any String value and check. If it does not work then paste your whole LogCat please.

EDIT : As per your latest LogCat update I think you have passed this as your Context. you need to pass getActivity() in place of 'this' as context

shivamDev31
  • 469
  • 1
  • 8
  • 23
0

You have to clear the back stack first !

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
Radoslav
  • 1,446
  • 1
  • 16
  • 30
  • unfortunately it does not fix my problem. The app is still closing when I press the back button on my phone. – RyuZz Feb 05 '15 at 14:52