4

I have created a fragment with action bar menu, that menu was shown but not working when its clicked.

Here is My Fragment:

public class ComposeFragment extends Fragment {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_compose, container, false);

        userName = (TextView) view.findViewById(R.id.user_name);
        subjectSpinner = (Spinner) view.findViewById(R.id.subject_spinner);
        sendButton = (Button) view.findViewById(R.id.send_btn);
        messageEditText = (EditText) view.findViewById(R.id.message);

        userName.setText(Ezcation.getInstance().userName);
        return view;
    }
@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.compose_menu, menu);
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("Menu","Before Switch");
        switch (item.getItemId()){
            case R.id.sent:
                Log.e("Menu","Sent");
                if (messageEditText.getText().toString().equals("")){
                    messageEditText.setError("Please Enter your Message");
                }else {
                    sendMessage(messageEditText.getText().toString());
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.messageActivity = (MessageActivity) context;
        SpannableString s = new SpannableString("Compose Message");
        s.setSpan(new TypefaceSpan(messageActivity, "Miller-Text.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        messageActivity.setTitle(s);
    }
}

When menu was clicked even Log.e("Menu","Before Switch"); not working.

My Menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/sent"
        android:title="Sent"
        android:orderInCategory="10"
        android:icon="@drawable/sent"
        app:showAsAction="ifRoom" />
</menu>

3 Answers3

7

For Future visiters you should use this in Activity class:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

To work properly hardcoding false isnt the right way

JAAD
  • 12,349
  • 7
  • 36
  • 57
  • "Hardcoding" sounds like "taboo". I always thought returning "true" means my method handled the event and returning "false" means I'm not interested, let the next one have their chance. Is it really more than a matter of style? That is, if I know there's another interested party. – Bö macht Blau Aug 31 '16 at 05:49
  • @0X0nosugar from the method docs Derived classes should call through to the base class for it to perform the default menu handling. – JAAD Aug 31 '16 at 05:55
  • @0X0nosugar well you are right about true and false but my point was about hardcoding only – JAAD Aug 31 '16 at 05:58
  • thanks :) I've been thinking these last minutes the last sentence in my other comment is a good reason why calling super is considered a good practice. Maintenance and so on. – Bö macht Blau Aug 31 '16 at 06:00
1

add this in your Activity.

@Override public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        break;
}
return super.onOptionsItemSelected(item);;
}
shinilms
  • 1,494
  • 3
  • 22
  • 35
  • If your Activity's onOptionsItemSelected method returs true, the call is consumed in activity and Fragment's onOptionsItemSelected is not called. So, return false in your Activity onOptionsItemSelected method or parent class implementation via super.onOptionsItemSelected call (default implementation returns false). – shinilms Aug 31 '16 at 05:16
  • I think you have to return false from the Activity method – Bö macht Blau Aug 31 '16 at 05:33
  • yea thats what I meant. FragmentActivity is the activity which the fragment is connected to. – shinilms Aug 31 '16 at 05:35
  • Well, one can get it wrong, obviously :-( Perhaps say someting like "the hosting activity"? – Bö macht Blau Aug 31 '16 at 05:36
0

The super call is missing inside onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.compose_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68