@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    setHasOptionsMenu(true);
    EventBus.getDefault().register(this);

    getAccountTask = new GetAccountTask(getActivity(), username);
    getAccountTask.setTaskProcessCallback(
        null); // Disable default loading indicator, we have our own
    getAccountTask.execute();

    getProfileFormDescriptionTask =
        new GetProfileFormDescriptionTask(getActivity()) {
          @Override
          protected void onSuccess(@NonNull FormDescription formDescription) throws Exception {
            EditUserProfileFragment.this.formDescription = formDescription;
            if (null != viewHolder) {
              setData(account, formDescription);
            }
          }
        };
    getProfileFormDescriptionTask.setTaskProcessCallback(null);
    getProfileFormDescriptionTask.execute();
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
    getAccountTask.cancel(true);
    getProfileFormDescriptionTask.cancel(true);
    if (null != setAccountImageTask) {
      setAccountImageTask.cancel(true);
    }
    helper.deleteTemporaryFile();

    EventBus.getDefault().unregister(this);
  }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    switch (requestCode) {
      case CAPTURE_PHOTO_REQUEST:
        {
          final Uri imageUri = helper.getImageUriFromResult();
          if (null != imageUri) {
            startActivityForResult(
                CropImageActivity.newIntent(getActivity(), imageUri, true), CROP_PHOTO_REQUEST);
          }
          break;
        }
      case CHOOSE_PHOTO_REQUEST:
        {
          final Uri imageUri = data.getData();
          if (null != imageUri) {
            startActivityForResult(
                CropImageActivity.newIntent(getActivity(), imageUri, false), CROP_PHOTO_REQUEST);
          }
          break;
        }
      case CROP_PHOTO_REQUEST:
        {
          final Uri imageUri = CropImageActivity.getImageUriFromResult(data);
          final Rect cropRect = CropImageActivity.getCropRectFromResult(data);
          if (null != imageUri && null != cropRect) {
            executePhotoTask(
                new SetAccountImageTask(getActivity(), username, imageUri, cropRect) {
                  @Override
                  protected void onSuccess(Void aVoid) throws Exception {
                    hideLoading();
                  }

                  @Override
                  protected void onException(Exception e) throws RuntimeException {
                    super.onException(e);
                    hideLoading();
                  }

                  private void hideLoading() {
                    if (null != viewHolder) {
                      viewHolder.profileImageProgress.setVisibility(View.GONE);
                    }
                  }
                });
            segment.trackProfilePhotoSet(CropImageActivity.isResultFromCamera(data));
          }
          break;
        }
      case EDIT_FIELD_REQUEST:
        {
          final FormField fieldName =
              (FormField) data.getSerializableExtra(FormFieldActivity.EXTRA_FIELD);
          final String fieldValue = data.getStringExtra(FormFieldActivity.EXTRA_VALUE);
          executeUpdate(fieldName, fieldValue);
          break;
        }
    }
  }
 @Override
 public void onDestroyView() {
   super.onDestroyView();
   viewHolder = null;
 }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    viewHolder = new ViewHolder(view);
    viewHolder.profileImageProgress.setVisibility(View.GONE);
    viewHolder.username.setText(username);
    final IconDrawable icon =
        new IconDrawable(getActivity(), FontAwesomeIcons.fa_camera)
            .colorRes(getActivity(), R.color.disableable_button_text)
            .sizeRes(getActivity(), R.dimen.fa_x_small);
    icon.setTintList(
        null); // IconDrawable is tinted by default, but we don't want it to be tinted here
    TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(
        viewHolder.changePhoto, icon, null, null, null);
    viewHolder.changePhoto.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            final PopupMenu popup = new PopupMenu(getActivity(), v);
            popup.getMenuInflater().inflate(R.menu.change_photo, popup.getMenu());
            popup.setOnMenuItemClickListener(
                new PopupMenu.OnMenuItemClickListener() {
                  public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                      case R.id.take_photo:
                        {
                          startActivityForResult(
                              helper.createCaptureIntent(getActivity()), CAPTURE_PHOTO_REQUEST);
                          break;
                        }
                      case R.id.choose_photo:
                        {
                          final Intent galleryIntent =
                              new Intent().setType("image/*").setAction(Intent.ACTION_GET_CONTENT);
                          startActivityForResult(galleryIntent, CHOOSE_PHOTO_REQUEST);
                          break;
                        }
                      case R.id.remove_photo:
                        {
                          executePhotoTask(
                              new DeleteAccountImageTask(getActivity(), username) {
                                @Override
                                protected void onSuccess(Void aVoid) throws Exception {
                                  hideLoading();
                                }

                                private void hideLoading() {
                                  if (null != viewHolder) {
                                    viewHolder.profileImageProgress.setVisibility(View.GONE);
                                  }
                                }
                              });
                          break;
                        }
                    }
                    return true;
                  }
                });
            popup.show();
          }
        });
    setData(account, formDescription);
  }