/**
   * Called when you are no longer visible to the user. You will next receive either onRestart(),
   * onDestroy(), or nothing, depending on later user activity.
   */
  @Override
  public void onStop() {
    super.onStop();

    if (mGetUserFriendsTask != null) {
      mGetUserFriendsTask.cancel(true);
    }
  }
  /**
   * Called when the activity is starting.
   *
   * @param savedInstanceState If the activity is being re-initialized after previously being shut
   *     down then this Bundle contains the data it most recently supplied in
   *     onSaveInstanceState(Bundle). Otherwise it is null.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectusercontacts);

    // Set up the ActionBar.
    mActionBar.setTitle("Compose Message");
    mActionBar.setSubtitle("Select Individual Recipients");

    // Set up the Side Navigation Menu.
    mSideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
    mSideNavigationView.setMenuItems(R.menu.side_navigation_menu);
    mSideNavigationView.setMenuClickCallback(this);
    mSideNavigationView.setMode(Mode.LEFT);

    // Variable initialization.
    mMembersList = new ArrayList<GroupMemberData>();
    mSelectedMembers = new ArrayList<GroupMemberData>();

    // Get a handle on the ListView.
    mListView = getListView();

    // Create our adapter for the ListView.
    mAdapter =
        new ArrayAdapter<GroupMemberData>(
            this, android.R.layout.simple_list_item_multiple_choice, mMembersList);

    // Inflate and get a handle on our loading button and indicator.
    mFooter = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.load_more, null);
    mLoadMoreButton = (Button) mFooter.findViewById(R.id.load_more_button);
    mLoadingIndicator = (ProgressBar) mFooter.findViewById(R.id.loading_indicator);

    // When the load more button is clicked, show the loading indicator and load more
    // group members.
    mLoadMoreButton.setOnClickListener(
        new OnClickListener() {

          /**
           * Called when a view has been clicked.
           *
           * @param v: The view that was clicked.
           */
          @Override
          public void onClick(View v) {
            mLoadMoreButton.setVisibility(View.GONE);
            mLoadingIndicator.setVisibility(View.VISIBLE);
            mGetUserFriendsTask = new GetUserFriendsTask();
            mGetUserFriendsTask.execute();
          }
        });

    // Add our loading button and indicator to the ListView.
    mListView.addFooterView(mFooter);

    // Set our ListView adapter.
    getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    setListAdapter(mAdapter);

    final Button doneButton = (Button) findViewById(R.id.done_button);
    doneButton.setOnClickListener(
        new View.OnClickListener() {

          /**
           * Called when a view has been clicked.
           *
           * @param v: The view that was clicked.
           */
          @Override
          public void onClick(View v) {
            mLoadMoreButton.setVisibility(View.INVISIBLE);
            Gson gson = new Gson();
            String userData = gson.toJson(mSelectedMembers);
            Intent intent = new Intent();

            intent.putExtra("userData", userData);
            setResult(Activity.RESULT_OK, intent);
            finish();
          }
        });

    // Get the first 15 friends.
    mGetUserFriendsTask = new GetUserFriendsTask();
    mGetUserFriendsTask.execute();
  }