Im trying to have a context menu inside of a dialog, when long clicking the TextView the context menu opens but when i click the items in the menu the menu closes and I dont get any response. After trying some things out I figured out the onContextItemSelected isnt getting called.

I tried solving it myself and failed and after many hours of searching i couldnt find a working solution online. everything i have found seems to be either a bit of topic or completely outdated. would love to get some help with it it really bothers me.

public class MainActivity extends AppCompatActivity {

    TextView menu;
    EditText prepTime, rating;
    Button add, newDessertButton;
    ListView list;

    ArrayList<Dessert> desserts;
    DessertAdapter dessertAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startUI();

    }

    private void startUI() {

        list = findViewById(R.id.list);
        newDessertButton = findViewById(R.id.addDessertButton);
        newDessertButton.setOnClickListener(newDessertButton -> {
            startDialog();
        });

        desserts = new ArrayList<Dessert>();
    }

    private void startDialog() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        View dialogViews = getLayoutInflater().inflate(R.layout.dialog_layout, null);

        menu = dialogViews.findViewById(R.id.menu);
        registerForContextMenu(menu);

        prepTime = dialogViews.findViewById(R.id.prepTime);
        rating = dialogViews.findViewById(R.id.rating);
        add = dialogViews.findViewById(R.id.addB);

        alertDialog.setView(dialogViews);
        alertDialog.setCancelable(true);

        AlertDialog dialog = alertDialog.create();
        dialog.show();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu_items, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(this, "yay", Toast.LENGTH_SHORT).show();
        int id = item.getItemId();
        menu.setText(item.getTitle());
        return super.onContextItemSelected(item);
    }

}

Source: View source