Question

I've done the tutorials for the Facebook SDK for Android (Especially the "Show Friends"-tutorial).

How can I mark the selected users which i've selected before at the PickerActivity when i click on the "Show Friends"-button again?

Was it helpful?

Solution 2

You have to pass comma separated string with selected facebook user ids in the bundle to the FriendPickerFragment.

Bundle args = new Bundle();
args.putString("com.facebook.android.PickerFragment.Selection", "11111, 2222, 333, already selected facebook ids....");
friendPickerFragment = new FriendPickerFragment(args);

In PickerFragment's onActivityCreated() it will parse the selected ids shows as selected in list. You can see the following code in PickerFragment in FacebookSDK.

selectionStrategy.readSelectionFromBundle(savedInstanceState, SELECTION_BUNDLE_KEY);

OTHER TIPS

I've looked at Facebook SDK's source code and it seems to me that PickerFragment does not give you possibility to reselect previously selected items. Since Facebook SDK is published under Apache License 2.0 and you have access to full source code I guess you could try to modify PickerFragment in such a way that it will have necessary methods.

Here is a solution which worked for me. I extend a standard FriendPickerFragment.

public class FriendPickerFragment2 extends FriendPickerFragment {

SelectionStrategy selectionStrategy;
String mPreSelectedIDs;

public FriendPickerFragment2(Bundle args)
{
    super(args);
}

@Override
SelectionStrategy createSelectionStrategy() {
    selectionStrategy = getMultiSelect() ? new MultiSelectionStrategy() : new SingleSelectionStrategy();
    return selectionStrategy;
}

public void showInitialSelection()
{
    Bundle bundle = new Bundle();
    bundle.putString(this.MULTI_SELECT_BUNDLE_KEY, mPreSelectedIDs);
    selectionStrategy.readSelectionFromBundle(bundle, this.MULTI_SELECT_BUNDLE_KEY);
    adapter.notifyDataSetChanged();
}

public void setInitialSelection(String IDs)
{
    mPreSelectedIDs = IDs;
}

}

I use FriendPickerFragment2 as a normal FriendPickerFragment. In OnCreate I do the following:

        FragmentManager fm = getSupportFragmentManager();

    if (savedInstanceState == null) {
        final Bundle args = getIntent().getExtras();
        friendPickerFragment = new FriendPickerFragment2(args);
        friendPickerFragment.setInitialSelection(pickedUsersString());

        fm.beginTransaction()
                .add(R.id.friend_picker_fragment, friendPickerFragment)
                .commit();
    } else {
        friendPickerFragment = (FriendPickerFragment2) fm.findFragmentById(R.id.friend_picker_fragment);
    }

Here pickedUsersString is a coma separated string of IDs.

The last point is to add one row in the OnStart:

protected void onStart() {
    super.onStart();
    try {

        friendPickerFragment.loadData(false);
        friendPickerFragment.showInitialSelection();
    } catch (Exception ex) {
        onError(ex);
    }
}

This solution worked for me.

Good news. In the current SDK version (3.6) the following feature was added:

  • Added setSelection methods on FriendPickerFragment to allow pre-selection of friends.
  • Added example use of setSelection API in Friend Picker Sample

FriendPickerFragment has this method:

/**
 * Sets the list of friends for pre selection. These friends will be selected by default.
 * @param userIds list of friends as ids
 */
public void setSelectionByIds(List<String> userIds) {
    preSelectedFriendIds.addAll(userIds);
}

And this method:

public void setSelection(List<GraphUser> graphUsers) {
    List<String> userIds = new ArrayList<String>();
    for(GraphUser graphUser: graphUsers) {
        userIds.add(graphUser.getId());
    }
    setSelectionByIds(userIds);
}

You should call one of these methods as soon as fragment instantiation:

    friendPickerFragment = new FriendPickerFragment(args);
    friendPickerFragment.setSelectionByIds(fbIDs);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top