CrimeListActivity Menu: New Crime label diappears after back


#1

Hi All,

Thank you to everyone who has replied to my multiple posts so far. One last one for good measure. I’m having an issue where the menu displays perfectly on load of the application Item 1 is a New Crime and Item 2 is Show Subtitle. However, when I add a new Crime and press the back button, I notice that the menu item labels all get messed up Item 1 says Hide Subtitle (but links to New Crime) and Item 2 says Show Subtitle.

CrmeListFragment as follows:

package com.example.criminalintent;
//This is the Fragment which will be shown in CrimeListActivity
import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;


public class CrimeListFragment extends ListFragment {
	private ArrayList<Crime> mCrimes;
	private boolean mSubtitleVisible;
	private static final String TAG = "CrimeListFragment";
	private Button createCrimeButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
				//getActivity().getActionBar().show();
		getActivity().setTitle(R.string.crimes_title); 
		mCrimes = CrimeLab.get(getActivity()).getCrimes();
		
				
		CrimeAdapter adapter = new CrimeAdapter(mCrimes);
		setListAdapter(adapter);
		setRetainInstance(true);
		mSubtitleVisible = false;
	}
	
	// This saves the state of the how/hide subtitle
	@TargetApi(11)
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstance){
		View v = inflater.inflate(R.layout.fragment_crime_list, parent, false);
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			if (mSubtitleVisible) {
				((ActionBarActivity)getActivity()).getSupportActionBar().setSubtitle(R.string.subtitle);
			}
		}
		
		createCrimeButton = (Button)v.findViewById(R.id.fragment_crime_create);
				createCrimeButton.setOnClickListener(new OnClickListener() {
				
					@Override
					public void onClick(View v) {
						callCreateCrime();
					}
				});
		
		return v;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id){
		Crime c = (Crime)(getListAdapter()).getItem(position);
		Log.d(TAG, c.getTitle() + " was clicked");
		
		//Start CriminalPagerActivity
		Intent i = new Intent(getActivity(), CrimePagerActivity.class);
		i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId());
		startActivity(i); 
	}
	
	private class CrimeAdapter extends ArrayAdapter<Crime> {
		
		public CrimeAdapter(ArrayList<Crime> crimes){
			super(getActivity(), 0, crimes);
		}
		
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// if no view, inflate one. 
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
			}
		
			//Configure the view for this Crime
			Crime c = mCrimes.get(position); 
		
			TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);
			titleTextView.setText(c.getTitle());
			TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);
			dateTextView.setText(c.getDate().toString());
			CheckBox solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
			solvedCheckBox.setChecked(c.getSolved());
		
			return convertView;
			}
		}
	
	@Override
	public void onResume() {
		super.onResume();
		((CrimeAdapter)getListAdapter()).notifyDataSetChanged(); 
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.fragment_crime_list, menu);
		MenuItem showSubtitle = menu.findItem(R.id.menu_item_show_subtitle);
		if(mSubtitleVisible && showSubtitle != null){
			showSubtitle.setTitle(R.string.hide_subtitle);
		}
	}
	
	@TargetApi(11)
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		
		switch(item.getItemId()) {
		case R.id.menu_item_new_crime:
			callCreateCrime();
		case R.id.menu_item_show_subtitle:
			final Activity a = getActivity();
			
			if(((ActionBarActivity)getActivity()).getSupportActionBar().getSubtitle() == null) {
				((ActionBarActivity)getActivity()).getSupportActionBar().setSubtitle(R.string.subtitle);
				mSubtitleVisible = true;
				item.setTitle(R.string.hide_subtitle);
			} else {
				((ActionBarActivity)getActivity()).getSupportActionBar().setSubtitle(null); 
				mSubtitleVisible = false;
				item.setTitle(R.string.show_subtitle);
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
					
		}
	}
	
	public void callCreateCrime(){
		Crime crime = new Crime(); 
		CrimeLab.get(getActivity()).addCrime(crime);
		Intent i = new Intent(getActivity(), CrimePagerActivity.class);
		i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
		startActivityForResult(i, 0);
	}
	
	
}

Any ideas? Would appreciate it.

Thank You in Advance.
Paul