How to not Create Crime if user pressed back

As I understand CrimeListFragment start to create a new Crime by calling method addCrime(). However if user didn’t put Title and if you just press back button on phone it will create a new Crime with empty title. So to avoid this what should I do.

Please explain me how OnBackPressed works. And any ideas to make it better?

In CrimeFragment, you could check the crime when you navigate back to see if the crime is empty. If it is, delete it.

Responding to back button presses in a fragment is a little tricky. The Activity really owns that type of navigation. There is an onBackPressed method on the activity that you could override and then call methods on your fragment. Instead of onBackPressed, you may want to override onFinish() in the Activity and call through to the fragment from there.

onBackPressed will only be called when you hit the back button. onFinish will get called any time the activity is finished (including from the up arrow in the top left).

@Override
public void onPause() {
    super.onPause();
    CrimeLab.get(getActivity()).updateCrime(mCrime);
    if (mCrime.getTitle() == null) {
        TaskLab.get(getActivity()).deleteCrime(mCrime);
        getActivity().finish();
    }
}

I did this way. How does this looks?

Downside here is that onPause is called in situations other than navigating back. For example, if you turn off the phone’s screen and then turn it back on, onPause will get called. Same thing if you switch to another app.

Ooops, you are right. I’ve just tried to turn off the phone’s screen and it suddenly disappeared.
What about onFinish(); How to work with that?

You’d have to override onFinish in your activity and from the Activity, call a method on the fragment.

How to create @Override method - onFinish(). Do you mean that I need to create method, which call a method onDestroy inside. I did not get it. Could you give example, please.

What about ContentProvider is that the best way to develop App. Using URI to acess to DataBase?

Looks like the method is called finish. In your activity, you can override that method just like you have overriden other methods:

@Override 
public void finish() {
  super.finish();
  // Your code here
}

When this happens, you would need to call a method on your fragment and that will be a little trickier to do.

> @Override
public void finish() {
    super.finish();
    UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
    CrimeLab.get(getApplicationContext()).deleteCrime(crimeId);
}

Thanks a lot. I have solved this problem. Now back works fine.
You are absolutely right I is tricky.
What about arguments do I need to set arguments to my CrimeFragment and put into Title and then return to CrimeActivity so it will check that title is setted.??

What if I start to insert to a database not from FragmentCrimeList, but from FragmentCrime and add some button that will pass all input data and inserts to the database?

What if I create a button which will send the result to a database? And if user click back or press a button on a smartphone it will delete the Crime. So I can put a validation to this button so the code will be optimized. What do you think?

I have solved the problem. I created a button which sends the result of crime and validates if there putted some value. In my CrimeActivity I get accessed to the fragment through FragmentManager

@Override
public void finish() {
    super.finish();
    String crimeCheckTitle;

    FragmentManager fm = getSupportFragmentManager();
    CrimeFragment fragment = (CrimeFragment) fm.findFragmentById(R.id.fragmentContainer);

    crimeCheckTitle = fragment.getExtraCheckTitle();

    if(crimeCheckTitle == null) {
        UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
        CrimeLab.get(getApplicationContext()).deleteCrime(crimeId);
    } else if (crimeCheckTitle.isEmpty()) {
        UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
        CrimeLab.get(getApplicationContext()).deleteCrime(crimeId);
    }
}

There my CrimeFragment code:

private ImageButton mSendImageButton;
private String mExtraCheckTitle;

>  mSendImageButton = (ImageButton) v.findViewById(R.id.send_image_button);
>         mSendImageButton.setOnClickListener(new View.OnClickListener() {
>             @Override
>             public void onClick(View v) {
>                 String extraString = mTitleField.getText().toString();
>                 if (extraString.length() == 0) {
>                     return;
>                 }
>                 setExtraCheckTitle(extraString);
>                 getActivity().finish();
>             }
>         });

public void setExtraCheckTitle(String sendTitle) {
    mExtraCheckTitle = sendTitle;
}
public String getExtraCheckTitle() {
    if (mExtraCheckTitle != null) {
        return mExtraCheckTitle = mCrime.getTitle();
    }
    return mExtraCheckTitle;
}

What do you think Sir?

Could you help. I wrote the code above but I found that when you open the CrimeActivity again (i.e. it is already has been created and in DB) and press the back key it is suddenly deletes from a DB. Why is that happened?

I would use the debugger to figure out what’s going on. Set a few breakpoints and inspect your code to see when the deleteCrime method is being called and what causes it to happen.