@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   switch (requestCode) {
     case ACTIVITY_CREATE:
       if (resultCode == RESULT_OK) {
         ToDo toDo = ToDoLoader.loadFromExtras(data);
         listFragment.repository.add(toDo);
       }
       break;
     case ACTIVITY_EDIT:
       if (resultCode == RESULT_OK) {
         ToDo newToDo = ToDoLoader.loadFromExtras(data);
         int index = data.getIntExtra(ToDoEditFragment.ARG_INDEX, -1);
         listFragment.repository.update(index, newToDo);
       }
       break;
   }
 }
  @Override
  public void onItemSelected(ToDo toDo) {
    if (isTwoPane) {
      Bundle arguments = new Bundle();
      ToDoLoader.writeToBundle(arguments, toDo);
      ToDoDetailFragment fragment = new ToDoDetailFragment();
      currentFragment = fragment;

      fragment.setArguments(arguments);
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      transaction.replace(R.id.todo_container, fragment);
      transaction.commit();
    } else {
      Intent detailIntent = new Intent(this, ToDoDetailActivity.class);
      ToDoLoader.writeToExtras(detailIntent, toDo);
      startActivity(detailIntent);
    }
  }
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info =
        (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {
      case R.id.action_edit_item:
        ToDo toDo = listFragment.repository.getItem(info.position);
        if (isTwoPane) {
          listFragment.setActivatedPosition(info.position);
          Bundle arguments = new Bundle();
          arguments.putInt(ToDoEditFragment.ARG_INDEX, info.position);
          ToDoLoader.writeToBundle(arguments, toDo);
          ToDoEditFragment fragment = new ToDoEditFragment();
          currentFragment = fragment;

          fragment.setArguments(arguments);
          FragmentTransaction transaction = getFragmentManager().beginTransaction();
          transaction.replace(R.id.todo_container, fragment);
          transaction.commit();
        } else {
          Intent editIntent = new Intent(this, ToDoEditActivity.class);
          editIntent.putExtra(ToDoEditFragment.ARG_INDEX, info.position);
          ToDoLoader.writeToExtras(editIntent, toDo);
          startActivityForResult(editIntent, ACTIVITY_EDIT);
        }
        break;
      case R.id.action_remove_item:
        listFragment.repository.remove(info.position);
        if (isTwoPane) {
          cancelAction();
        }
        break;
      default:
        return super.onContextItemSelected(item);
    }
    return true;
  }