title: Accessing contacts list android
body: <p>I have managed to access the contacts list to pick a contact and put number in the editText. The only problem I am facing is that the app stops when I press the back button while in the contact list. Can you please help?</p>
  
  <pre><code>Intent contactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
  @Override
  protected void onActivityResult(int reqCode, int resultCode, Intent intent) 
  {
      super.onActivityResult(reqCode, resultCode, intent);
      switch (reqCode) {
      case ADDRESS_REQUEST2:
          EditText edittext2 = (EditText) findViewById(R.id.number2);
          Uri uri2 = intent.getData();
          Cursor cursor2 = getContentResolver().query(uri2, null, null, null, null);
          cursor2.moveToFirst();
          String phoneNumber2 = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  
          edittext2.setText(phoneNumber2);
          break;
      case ADDRESS_REQUEST3:
          EditText edittext3 = (EditText) findViewById(R.id.number3);
          Uri uri3 = intent.getData();
          Cursor cursor3 = getContentResolver().query(uri3, null, null, null, null);
          cursor3.moveToFirst();
          String phoneNumber3 = cursor3.getString(cursor3.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  
          edittext3.setText(phoneNumber3);
          break;
      case ADDRESS_REQUEST4:
          EditText edittext4 = (EditText) findViewById(R.id.number4);
          Uri uri4 = intent.getData();
          Cursor cursor4 = getContentResolver().query(uri4, null, null, null, null);
          cursor4.moveToFirst();
          String phoneNumber4 = cursor4.getString(cursor4.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  
          edittext4.setText(phoneNumber4);
          break;
      default:
          EditText edittext1 = (EditText) findViewById(R.id.number1);
          Uri uri1 = intent.getData();
          Cursor cursor1 = getContentResolver().query(uri1, null, null, null, null);
          cursor1.moveToFirst();
          String phoneNumber1 = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          edittext1.setText(phoneNumber1);
          break;
      }
  
  
  }
  </code></pre>