How to perform the click of Actionbar item in Sherlock Fragment in Android?

I want to perform a click on actionbar item in my Sherlock Fragment. My item is showing on fragment class on action bar, but when I click on the item , the Toast message is not shown.

Here is my Sherlock Fragment code:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
         inflater.inflate(R.menu.profile_menu_items, menu);
         super.onCreateOptionsMenu(menu, inflater);
         
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
         switch (item.getItemId()) {
         case R.id.Online:
            // do s.th.
             Toast.makeText(getActivity(), "online", Toast.LENGTH_SHORT).show();
            return true;
         default:
        return super.onOptionsItemSelected(item);
    }
     


@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    
        super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
        
    }}

Here is the menu file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
 <item android:id="@+id/Online"
       android:showAsAction="always"
        android:title="Online"/>
</menu>

Solution 1:

You can use interface for this, create one interface and in your MainActivity implements this interface, like in my case it is -

public class MainActivity extends Activity implements SwitherInterface{
// rest of the code with implementation of changeToDrawer()
}

Define your interface -

public interface SwitherInterface {
    public void changeToDrawer();
}

Inside your fragment declare interface and initialize this in onAttach method -

SwitherInterface switcher;

@Override
    public void onAttach(Activity a) {
        super.onAttach(a);
        switcher = (SwitherInterface) a;
    }

And you can then use the declared methods directly by -

switcher.changeToDrawer();

Hope this helps you.