Question

I am working in Sherlock Fragment and needed to add some options in the optionMenu. But unable to do this. Here is my code below. i have already added setHasOptionMenu(true) in onCreateView :So please have a look at this code,,, and tell me where i am wrong

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
 {
        super.onCreateOptionsMenu(menu, inflater);

        progress = menu.add("Progress");// first option
        progress.setIcon(android.R.id.progress);
        progress.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        MenuItem add = menu.add("Refresh");// second option
        add.setIcon(R.drawable.ic_menu_refresh);
        add.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        MenuItem get = menu.add("Logout");// third option
        get.setIcon(R.drawable.power);
        get.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);



        get.setOnMenuItemClickListener(new OnMenuItemClickListener() 
        {

            @Override
            public boolean onMenuItemClick(MenuItem item)
            {

                userfunctions = new UserFunctions();
                       //AsyncTask
                class AsyncLogout extends AsyncTask<Void, Void, String> {

                    protected String doInBackground(Void... params) {
                        json = userfunctions.logoutUser(userid);



                        try {
                            if (json.getString(KEY_SUCCESS) != "1") {
                                String res = json.getString(KEY_SUCCESS);

                                if (Integer.parseInt(res) == 1) {
                                    p_error_msg = "successful";

                                }

                            } else {
                                p_error_msg = "error";

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        return p_error_msg;
                    }

                    protected void onPostExecute(String result) {
                        try {
                            if (result.equals("successful")) {

                                Intent ii = new Intent(getActivity(),
                                        HomeActivity.class);

                                SharedPreferences.Editor editor = mPrefs.edit();

                                editor.putString("login", "0");

                                editor.remove("PREF_ACCESS_TOKEN_TWITTER");
                                editor.remove("PREF_ACCESS_TOKEN_SECRET_TWITTER");
                                editor.remove("PREF_ACCESS_TOKEN_FB");
                                editor.remove("PREF_ACCESS_TOKEN_SECRET_FB");

                                editor.commit();

                                startActivity(ii);
                                getActivity().finish();
                            } else {

                            }

                        } catch (Exception e) {

                        }

                    }

                    protected void onPreExecute() {
                        super.onPreExecute();

                    }
                }

                AsyncLogout as=new AsyncLogout();
                as.execute();

                return false;
            }
        });

        add.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) 
            {
                Intent i = new Intent(getActivity(), MainActivity.class);
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("login", "1");
                editor.putInt("defaultSelector", 2);
                editor.commit();
                startActivity(i);
                getActivity().finish();

                return false;
            }
        });



 }
Was it helpful?

Solution

Here is the proper way how to handle MenuItems from Fragment :

    public class MyFragment extends Fragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_inner_cards, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch(item.getItemId()){
        case R.id.action_sort:
                // DO STUFF 
            break;
        case R.id.action_anytag:
                // DO STUFF
            break;
        }
        return true;
    }
}

My suggestion is, instead of creating the items in Java code, use a custom menu.xml only for your Fragment, inflate it and use onOptionsItemSelected() for handling click event on menu items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top