user2229747
user2229747

Reputation: 321

Actionbar launch fragment

How can I launch a fragment from onOptionsItemsSelected?

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
          case R.id.action_support:

              Fragment f = new SupportFragment();
                          FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(android.R.id.content, f).commit();



              Toast display = Toast.makeText(this, "Settings", 10);
              display.show();
          case R.id.action_guide:
              Toast display1 = Toast.makeText(this, "Guide", 10);
              display1.show();
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }

}

Im trying to launch the support fragment.

Thanks

Upvotes: 0

Views: 102

Answers (2)

Mahmoud Hashim
Mahmoud Hashim

Reputation: 550

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   Fragment fragment; 
    switch (item.getItemId()) {
            case R.id.action_support:
                fragment = new Home();
            case 1:
                fragment = new NotesList();
            default:
                fragment = new defaultPageOrAnyOtheOption();
    }

  FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment).commit();
  return true;

} // onOptionsItemSelected

Upvotes: 4

vinay Maneti
vinay Maneti

Reputation: 1451

Try it

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
          case R.id.action_support:

              //launch Support Fragment
             // Fragment f = new SupportFragment();
             // replace(android.R.id.content, f);

              Toast display = Toast.makeText(this, "Settings", 10);
              display.show();
          return true;
          case R.id.action_guide:
              Toast display1 = Toast.makeText(this, "Guide", 10);
              display1.show();
             return true;
                     }
    }

Upvotes: 0

Related Questions