5.5.5. Overridden Activity Method onOptionsItemSelected

Method onOptionsItemSelected (Fig. 5.22) is called when a menu item is selected. In this app, the default menu provided by the IDE when you created the project contains only the Settings menu item, so if this method is called, the user selected Settings. Line 106 creates an explicit Intent for launching the SettingsActivity. The Intent constructor used here receives the Context from which the Activity will be launched and the class representing the Activity to launch (SettingsActivity.class). We then pass this Intent to the inherited Activity method startActivity to launch the Activity.


102      // displays SettingsActivity when running on a phone
103      @Override
104      public boolean onOptionsItemSelected(MenuItem item)
105      {
106         Intent preferencesIntent = new Intent(this, SettingsActivity.class);
107         startActivity(preferencesIntent);
108         return super.onOptionsItemSelected(item);
109      }
110


Fig. 5.22 | MainActivity overridden Activity method onOptionsItemSelected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset