Buggy code:
public class UserSettingsActivity extends Activity { public static final int ADD_USER_REQUEST = 0 ; public static final int EDIT_USER_REQUEST = 1 ; private EditText txtUserName ; private EditText txtBodyHeight ; private EditText txtBirthday ; private RadioGroup radioScaleUnit ; private Button btnBirthdaySet ; private Button btnOk ; private Button btnCancel ; private Button btnDelete ; private SimpleDateFormat birthdayFormat = new SimpleDateFormat ( "dd.MM.yyyy" ) ; private Context context ; @ Override protected void onCreate ( Bundle savedInstanceState ) { } private void editMode ( ) { } private boolean validateInput ( ) { } private OnDateSetListener datePickerListener = new DatePickerDialog . OnDateSetListener ( ) { @ Override public void onDateSet ( DatePicker view , int selectedYear , int selectedMonth , int selectedDay ) { } } ; private class onClickListenerDelete implements View . OnClickListener { @ Override public void onClick ( View v ) { AlertDialog . Builder deleteAllDialog = new AlertDialog . Builder ( v . getContext ( ) ) ; deleteAllDialog . setMessage ( getResources ( ) . getString ( question_really_delete_user ) ) ; deleteAllDialog . setPositiveButton ( getResources ( ) . getString ( label_yes ) , new DialogInterface . OnClickListener ( ) { public void onClick ( DialogInterface dialog , int id ) { int userId = getIntent ( ) . getExtras ( ) . getInt ( "id" ) ; OpenScale openScale = OpenScale . getInstance ( context ) ; openScale . deleteScaleUser ( userId ) ; <START_BUG> openScale . clearScaleData ( ) ; <END_BUG> ArrayList < ScaleUser > scaleUser = openScale . getScaleUserList ( ) ; int lastUserId = - 1 ; if ( ! ( scaleUser . isEmpty ( ) ) ) { lastUserId = scaleUser . get ( 0 ) . id ; } SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( context ) ; prefs . edit ( ) . putInt ( "selectedUserId" , lastUserId ) . commit ( ) ; Intent returnIntent = new Intent ( ) ; setResult ( RESULT_OK , returnIntent ) ; finish ( ) ; } } ) ; deleteAllDialog . setNegativeButton ( getResources ( ) . getString ( label_no ) , new DialogInterface . OnClickListener ( ) { public void onClick ( DialogInterface dialog , int id ) { dialog . dismiss ( ) ; } } ) ; deleteAllDialog . show ( ) ; } } private class onClickListenerOk implements View . OnClickListener { @ Override public void onClick ( View v ) { if ( validateInput ( ) ) { OpenScale openScale = OpenScale . getInstance ( context ) ; String name = txtUserName . getText ( ) . toString ( ) ; int body_height = Integer . valueOf ( txtBodyHeight . getText ( ) . toString ( ) ) ; int checkedRadioButtonId = radioScaleUnit . getCheckedRadioButtonId ( ) ; int scale_unit = - 1 ; switch ( checkedRadioButtonId ) { case id . btnRadioKG : scale_unit = 0 ; break ; case id . btnRadioLB : scale_unit = 1 ; break ; case id . btnRadioST : scale_unit = 2 ; break ; } String date = txtBirthday . getText ( ) . toString ( ) ; int id = - 1 ; if ( ( getIntent ( ) . getExtras ( ) . getInt ( "mode" ) ) == ( UserSettingsActivity . EDIT_USER_REQUEST ) ) { id = getIntent ( ) . getExtras ( ) . getInt ( "id" ) ; openScale . updateScaleUser ( id , name , date , body_height , scale_unit ) ; } else { openScale . addScaleUser ( name , date , body_height , scale_unit ) ; id = openScale . getScaleUserList ( ) . get ( 0 ) . id ; } SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( context ) ; prefs . edit ( ) . putInt ( "selectedUserId" , id ) . commit ( ) ; Intent returnIntent = new Intent ( ) ; setResult ( RESULT_OK , returnIntent ) ; finish ( ) ; } } } private class onClickListenerBirthdaySet implements View . OnClickListener { @ Override public void onClick ( View v ) { Calendar cal = Calendar . getInstance ( ) ; DatePickerDialog datePicker = new DatePickerDialog ( context , datePickerListener , cal . get ( Calendar . YEAR ) , cal . get ( Calendar . MONTH ) , cal . get ( Calendar . DAY_OF_MONTH ) ) ; datePicker . show ( ) ; } } private class onClickListenerCancel implements View . OnClickListener { @ Override public void onClick ( View v ) { Intent returnIntent = new Intent ( ) ; setResult ( RESULT_CANCELED , returnIntent ) ; finish ( ) ; } } }

Fix:
openScale . clearScaleData ( userId ) ;