Search code examples
androidactionbarsherlockslidingmenu

How to use sliding menu toggle() from fragment


In my application I use Sherlock actionbar with sliding menu.

I can use the sliding menu toggle from Sherlock Fragment activity like this:

switch (item.getItemId()) {

    case android.R.id.home:
        toggle();
        break;}

but now I am trying to hide the actionbar and want to use the actionBar menu button from fragments.

I hide the actionbar, but how can I use the toggle() from fragments?

Can anyone give me an idea how can I use android.R.id.home action from fragment instead of fragmentActivity?


Solution

  • Two way to done this:

    1. In your Activity override onOptionsItemSelected and handle home button clicked and call super for other options menu. (new for home the toggle() will be called and other menu options you can handle them in the Fragment)

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case android.R.id.home:
              toggle();
              return true;
      
          default:
              return super.onOptionsItemSelected(item);
          }
      }
      
    2. define an interface in your Fragment and implements it on the Activity. Call the interface function on case android.R.id.home in the Fragment, then call toggle() in the Activity.