Potential Challenge Solution? Deleting from CrimeFragment


I added a menu called “challenge_fragment_crime_menu.xml” as seen below. I also added an icon in “drawable-hdpi” called ic_delete:

[b]<?xml version="1.0" encoding="utf-8"?>


The code I changed in CrimeFragment.java:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.challenge_delete:
return true;

case android.R.id.home:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.challenge_fragment_crime_menu, menu);


Is this a good way to do this?




Just a note:

Remember that switch-case functions work as follows:

  1. Evaluate Switch to proper Case.
  2. Run code from that point on (including the next cases) until ‘return’ call.

A clever way to reduce code in this challenge is to put the Delete case right before the NavUtil “up/home” case, and don’t put a ‘return’ statement after your delete code. This way, if you press ‘delete’, it will delete the crime, then automatically start running the “NavUtil Up/Home Navigation” code (with it’s OWN call to ‘return’), bringing you automatically to the ListView fragment/activity.

This only saves a couple lines of code, but a little trick to consider! :wink:


I did the same thing save for one exception: I set android:showAsAction to “never”, ensuring that the button is only available within the overflow menu. I’m just thinking that from a UX perspective, you don’t want an active delete button lying around on the screen for the user to accidentally press. There should have to be some concerted effort involved.


is there a benefit in not using the already existing crime_list_item_context.xml file that contains the delete item


I finish this challange
without this line

getActivity().finish(); return true;

what is the function of


and for the delete code in crimeListFragment.java
we need to do

for notify the adapter if the dataset have changed(get deleted)
but why in CrimeFragment not?