0

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

rosu alin
  • 5,674
  • 11
  • 69
  • 150

2 Answers2

1

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.

TNR
  • 5,839
  • 3
  • 33
  • 62
  • thanks, this is a huge step forward for me, it works, but now i have another question. I have 2 buttons, one for each menu, how to I select which one should the first button open, and then the second? I have tried: menu.showMenu(); menu.toggle(); for first one, and for the second: menu.showSecondaryMenu(); menu.toggle(); – rosu alin Jan 16 '13 at 14:30
  • @rosualin your second question is confusing. but will say you answer. when you have two buttons, append one slider to one button i.e. handle first button click for first sliding menu and second for second sliding menu. – TNR Jan 16 '13 at 14:34
  • yes, i understood that, i made clickListeners for the buttons, and how can i select which of those 2 sliding menu's to open with menu.toggle() ? – rosu alin Jan 16 '13 at 14:35
  • yes you need to use menu.toggle() for swapping the current state. – TNR Jan 16 '13 at 14:38
  • menu.toggle just opens the first sliding menu, and then, on the second command closes the first sliding menu. I have tried with showSecondaryMenu(), and showMenu();, but still, both buttons just show me the first menu – rosu alin Jan 16 '13 at 14:41
  • @rosualin What the idea I have explained you. don't know more about this sliding menu. – TNR Jan 16 '13 at 14:44
  • thanks a lot, i will keep on searching, and i will post the answer when I'll find it, also i will accept your answer after I figure out what is wrong, because the answer you gave me, is correct, but i still have the other issue, and i think more people will look over the question if it's not answered. Thank you again – rosu alin Jan 16 '13 at 14:48
  • I needed to put: menu.setMode(SlidingMenu.LEFT_RIGHT); so that it could have 2 menus – rosu alin Jan 16 '13 at 16:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22881/discussion-between-nagaraj436-and-rosu-alin) – TNR Jan 17 '13 at 07:15
  • @TNR, i'm facing problem in sliding. i.e if i add showsecondarymenu() instead of toggle i'm getting blank. could you please help me – Naruto Feb 20 '13 at 06:47
1

@LLL Following code is working Properly to me.i hope,it help u more...

    SlidingMenu slidingMenu  = getSlidingMenu();slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
    slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
    slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    slidingMenu.setFadeDegree(0.35f);
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    slidingMenu.setMenu(R.layout.profile);
    slidingMenu.setSecondaryMenu(R.layout.nextactivity);
    Button csButton=(Button)findViewById(R.id.txtx);
    inside csButton onclick listener just need to call slidingMenu.showSecondaryMenu();
    and, Button csButton1=(Button)findViewById(R.id.button1);
    inside csButton1 onclick listener just need to call slidingMenu.showMenu();
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
rkv
  • 131
  • 6