In the next steps, you will add the menu so that it can display a menu item (UI Event) for the main activity and the user can open this activity by taping on this menu item.
1. Add new menu folder to res folder then add menu_main.xml to this menu folder as follows.
2. Open menu_main.xml and modify it to have UI Event menu item as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_uievent"
android:title="UI Event"
app:showAsAction="never" />
</menu>
3. Open StartActivity.java, add the event listeners onCreateOptionsMenu and onOptionsItemSelected (see Week 1 lecture about how to add event listener at runtime and Week 2 lecture how to add menu to an Empty Activity)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_uievent:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("message", "Hello World!");
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Now you run the app and tap on the menu then on the menu item.
In the next steps, you will add the back arrow button so that the user can click it to go back to the previous activity.
3. Open AndroidManifest.xml file then add the following to the main activity
android:parentActivityName=".StartActivity"
4. Run your project and check if you see the back arrow button and it works.