Save files


I am not sure if I did or understood something wrong, but on CrimeFragment we save the crimes at onPause.

This save the crimes to a file when CrimeFragment is paused. So when we add a crime, as soon as the activity gets to the paused state, the crime is saved.

When I delete crimes, the change is not being saved anywhere, as the fragment is not CrimeFragment, but CrimeListFragment.

What I thought of doing is add a flag to CrimeLab to indicate whether the crimes files are up to date or not, set this flag to false on addCrime and deleteCrime and set it to true on the constructor and saveCrimes after successfully saving the crimes.

As to when to call saveCrimes, I found I can register a DataObserver on the CrimeAdapter and thought about overriding onChanged and saving there.
Or at onPause on both CrimeFragment and CrimeListFragment.

What are your thoughts on that?

public void deleteCrime(Crime c) {


[quote=“Beginner93”] public void deleteCrime(Crime c) { mCrimes.remove(c); saveCrimes(); } [/quote]

This would cause a lot of rapid saving if you’re removing several items via the multiple choice contextual action bar. Seems to me like it would be better put at the end of the onActionItemClicked callback.

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
	switch (item.getItemId()) {
			CrimeAdapter adapter = (CrimeAdapter) getListAdapter();
			CrimeLab lab = CrimeLab.getInstance(getActivity());
			for (int i = adapter.getCount()-1; i >= 0 ; i--) {
				if (getListView().isItemChecked(i)) {
			return true;
			return false;