Wie kann ich ein MenuItem in Android programmgesteuert auslösen / anklicken?

Ich habe diese Menüpunkte in meinemmenu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item android:id="@+id/action_restart" android:title="Restart"
        android:orderInCategory="1" />
    <item android:id="@+id/action_clear" android:title="Clear"
        android:orderInCategory="2" />
    <item android:id="@+id/action_update" android:title="Update"
        android:orderInCategory="3" />
    <item android:id="@+id/action_about" android:title="About"
        android:orderInCategory="4" />
    <item android:id="@+id/action_try_restart" android:title="Try Restart"
        android:orderInCategory="5" />
</menu>

Und ich habe dies in meinemonOptionsItemSelected Methode

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_restart) {
        Toast.makeText(MainActivity.this, "Restart...", Toast.LENGTH_LONG).show();
    }

    if (id == R.id.action_clear) {
        Toast.makeText(MainActivity.this, "Clear...", Toast.LENGTH_LONG).show();
    }

    if (id == R.id.action_update) {
        Toast.makeText(MainActivity.this, "Update...", Toast.LENGTH_LONG).show();
    }

    if (id == R.id.action_about) {
        Toast.makeText(MainActivity.this, "About...", Toast.LENGTH_LONG).show();
    }

    if(id == R.id.action_try_restart) {
        // how to click / trigger the "action_restart" from here?
    }

    return super.onOptionsItemSelected(item);
}

Ich habe versucht mit:

MenuItem actionRestart = (MenuItem) findViewById(R.id.action_restart);
actionRestart; //

AberactionRestart reference bietet nichts wieclick, trigger, etc

Ich möchte auch darauf hinweisen, dass ich neu in der Android-Entwicklung bin und über einen PHP / JavaScript-Hintergrund verfüge. Daher ist diese Java-OOP-Version für mich neu.

Antworten auf die Frage(16)

Ihre Antwort auf die Frage