The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Saving an Item

Having the EditFragment is nice, but we are not saving the changes anywhere. As soon as we leave the fragment, the “edits” vanish.

This is not ideal.

So, in this tutorial, we will allow the user to save their changes, by clicking a suitable action bar item.

This is a continuation of the work we did in the previous tutorial. The book’s GitHub repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Step #1: Adding the Action Bar Item

First, let’s set up the Save action bar item.

Right-click over res/drawable/ in the project tree and choose “New” > “Vector Asset” from the context menu. This brings up the Vector Asset Wizard. There, click the “Icon” button and search for save:

Android Studio Vector Asset Selector, Showing save Options
Figure 163: Android Studio Vector Asset Selector, Showing “save” Options

Choose the “save” icon and click “OK” to close up the icon selector. Then, click “Next” and “Finish” to close up the wizard and set up our icon.

Then, right-click over the res/menu/ directory and choose New > “Menu resource file” from the context menu. Fill in actions_edit.xml in the “New Menu Resource File” dialog, then click OK to create the file to open it in the menu editor. In the Palette, drag a “Menu Item” into the preview area. This will appear as an item in an overflow area:

Android Studio Menu Editor, Showing Added MenuItem
Figure 164: Android Studio Menu Editor, Showing Added MenuItem

In the Attributes pane, fill in save for the “id”. Then, choose both “ifRoom” and “withText” for the “showAsAction” option. Next, click on the “…” button next to the “icon” field. This will bring up an drawable resource selector — click on ic_save_black_24dp in the list of drawables, then click OK to accept that choice of icon.

Then, click the “…” button next to the “title” field. As before, this brings up a string resource selector. Click on “Add new resource” > “New string Value” in the drop-down towards the top. In the dialog, fill in menu_save as the resource name and “Save” as the resource value. Click OK to close the dialog, to complete our work on setting up the action bar item:

Android Studio Menu Editor, Showing Configured MenuItem
Figure 165: Android Studio Menu Editor, Showing Configured MenuItem

We also need to take steps to arrange to show this action bar item on EditFragment, as we did with DisplayFragment for the “edit” item.

Add this onCreate() method to EditFragment, to indicate that this fragment wishes to participate in the action bar:

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);
  }

Next, add this onCreateOptionsMenu() method to EditFragment, to inflate our newly-created menu resource:

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

    super.onCreateOptionsMenu(menu, inflater);
  }

If you run the app and edit a to-do item, you should see the new action bar item on the EditFragment:

ToDo App, EditFragment, with Save Action Bar Item
Figure 166: ToDo App, EditFragment, with Save Action Bar Item

Step #2: Replacing the Item

The preview of this section is being chased by zombies.

Step #3: Returning to the Display Fragment

The preview of this section was lost in the sofa cushions.

What We Changed

The preview of this section is in an invisible, microscopic font.