Search code examples
androidandroid-activitymenufragmentsliding

Adding 2 sliding menus to an activity


I have an activity, where using the Sliding Menu library, i try to create 2 sliding menus. This is the code i tried:

 FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.RIGHT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    menu.setShadowDrawable(R.drawable.shadow);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.tutorial_layout);
    rightSlide = new HelpFragment();
    t.replace(R.id.slidingList2, rightSlide);
    t.commit();
    menu.setSecondaryMenu(R.layout.log_history);
    leftSlide = new LogHistory();
    t.replace(R.id.loghistorycon, leftSlide);
    t.commit();

Now i get a ANR error, and Logcat says, that the FragmentTransaction t, has already been commited. I looked at the example from: github.com/jfeinstein10/SlidingMenu and it allows him to do 2 commit's:

 setContentView(R.layout.content_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, new SampleListFragment())
    .commit();

    getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
    getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame_two, new SampleListFragment())
    .commit();

What am I doing wrong? i just can't see the difference


Solution

  • Change your above code as below

     FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.RIGHT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.tutorial_layout);
        rightSlide = new HelpFragment();
        t.replace(R.id.slidingList2, rightSlide);
        t.commit();
        t = this.getSupportFragmentManager().beginTransaction();
        menu.setSecondaryMenu(R.layout.log_history);
        leftSlide = new LogHistory();
        t.replace(R.id.loghistorycon, leftSlide);
        t.commit();
    

    For a FragmentTransaction, you can have only one commit. In your code you created a FragmentTransaction object and called commit once for rightSlide. So t is not usable for transactions anymore. So you have create another FragmentTransaction as I have done in the above code. I hope this will work for you.