Save files


#1

Hello.
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?


#2
public void deleteCrime(Crime c) {
        mCrimes.remove(c);
        saveCrimes();
    }

#3

[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()) {
		case R.id.menu_item_delete_crime:
			CrimeAdapter adapter = (CrimeAdapter) getListAdapter();
			CrimeLab lab = CrimeLab.getInstance(getActivity());
			for (int i = adapter.getCount()-1; i >= 0 ; i--) {
				if (getListView().isItemChecked(i)) {
					lab.deleteCrime(adapter.getItem(i));
				}
			}
			mode.finish();
			adapter.notifyDataSetChanged();
			lab.saveCrimes();
			return true;
		default:
			return false;
	}
}