Photo Not Saving with "else if"... comments?


#1

I was banging my head against the wall trying to figure out why my REQUEST_PHOTO intent result wasn’t saving. I did a debug to trace that resultCode and REQUEST_PHOTO both were returning the same value ( “1”) but for some reason the “else if” portion of the statement never executed. Here is the code:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) return;
        if (requestCode == REQUEST_DATE) {
            Date date = (Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
            mCrime.setDate(date);
            updateDate();
        else if (requestCode == REQUEST_PHOTO) {
            // create a new Photo object and attach it to the crime
            String filename = data
                .getStringExtra(CrimeCameraFragment.EXTRA_PHOTO_FILENAME);
            if (filename != null) {
                Photo p = new Photo(filename);
                mCrime.setPhoto(p);
                Log.i(TAG, "Crime: " + mCrime.getTitle() + " has a photo.");
            }
        }
    }

I was able to get it to work by changing the nested “if” statement to a switch block:

@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		if (resultCode != Activity.RESULT_OK) return;
		switch (requestCode){
		case REQUEST_DATE:
			Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
			mCrime.setDate(date);
			updateDate();
			break;
		case REQUEST_PHOTO:
			String filename = data.getStringExtra(CrimeCameraFragment.EXTRA_PHOTO_FILENAME);
			Photo p = new Photo(filename);
			if (filename != null){
				mCrime.setPhoto(p);
				Log.i(TAG, "Crime: " + mCrime.getTitle() + " has a photo.");
			}
			
			break;
		}
        }

I can’t figure out why the “if” version didn’t work. Any thoughts?

…Rob


#2

I don’t understand how your first block of code even compiles. You need a } after updateDate() and before the “else if”.