Search code examples
androidfacebookfacebook-graph-apionactivityresultfacebook-friends

How to use FB FriendPickerFragment to show you list of all your friends in order to invite them to use your app


EDIT-----: So I did manage to make it show me users and be able to select some, and to send me back the selected users to my app but there is a slight problem. "Uri.parse("picker://friend");" doesn't give me the list of my friends, but only a list of the friends that I have and have my app installed.

I followed this example: https://developers.facebook.com/docs/android/scrumptious/show-friends but I am trying to modify it, so basically I took out the selectionActivity and Fragment. So I have from my activity a button that calls the PickerActivity which contains FriendPickerFragment. I can select my friends from there, but back in my activities onActivityResult i get back "data" as null.

I have an Application class in my app, where i save the FB session, and also have the login function in there.

In my MainActivity onCreate I have this:

   MyApp.getInstance().facebookLogin(PSAddFriendsActivity.this, new CrudStateCallback() {
        @Override
        public void onResponse(final String string) {
            Log.i("", "session : session is opened? : " + MyApp.getInstance().fbSession.getAccessToken());               
        }
    });

After having logged in, I instantiate a list with the current friends I have in my app, and the first position of this list is a FB button:

@Override
protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);
    if(position == 0){
            startPickerActivity(PSPickerActivity.FRIEND_PICKER, 0);
    }else if(position ==1){
        //TODO: OPEN CONTACTS PAGE TO ADD FRIENDS        
    }

}

This is my onACtivityResult and the "startPickerActivity" from this class:

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    Log.i("","--------------data is : " + data);
    Log.i("","--------------resultCode is : " + resultCode);
    Log.i("","--------------requestCode is : " + requestCode);

}

public void startPickerActivity(Uri data, int requestCode) {
    Intent intent = new Intent();
    intent.setData(data);
    intent.setClass(PSAddFriendsActivity.this, PickerActivity.class);
    startActivityForResult(intent, requestCode);
}

This is the PickerActivity, how I took it from FB:

public class PickerActivity extends FragmentActivity{
private FriendPickerFragment friendPickerFragment;
public static final Uri FRIEND_PICKER = Uri.parse("picker://friend");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pickers);

    Bundle args = getIntent().getExtras();
    FragmentManager manager = getSupportFragmentManager();
    Fragment fragmentToShow = null;
    Uri intentUri = getIntent().getData();

    if (FRIEND_PICKER.equals(intentUri)) {
        if (savedInstanceState == null) {
            friendPickerFragment = new FriendPickerFragment(args);
        } else {
            friendPickerFragment =
                    (FriendPickerFragment) manager.findFragmentById(R.id.picker_fragment);
        }
        // Set the listener to handle errors
        friendPickerFragment.setOnErrorListener(new PickerFragment.OnErrorListener() {
            @Override
            public void onError(PickerFragment<?> fragment,
                                FacebookException error) {
                PSPickerActivity.this.onError(error);
            }
        });
        // Set the listener to handle button clicks
        friendPickerFragment.setOnDoneButtonClickedListener(
                new PickerFragment.OnDoneButtonClickedListener() {
                    @Override
                    public void onDoneButtonClicked(PickerFragment<?> fragment) {
                        finishActivity();
                    }
                });
        fragmentToShow = friendPickerFragment;

    } else {
        // Nothing to do, finish
        setResult(RESULT_CANCELED);
        finish();
        return;
    }

    manager.beginTransaction()
            .replace(R.id.picker_fragment, fragmentToShow)
            .commit();
}

private void onError(Exception error) {
    onError(error.getLocalizedMessage(), false);
}

private void onError(String error, final boolean finishActivity) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.error_dialog_title).
            setMessage(error).
            setPositiveButton(R.string.error_dialog_button_text,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (finishActivity) {
                                finishActivity();
                            }
                        }
                    });
    builder.show();
}

private void finishActivity() {
    setResult(RESULT_OK, null);
    finish();
}

@Override
protected void onStart() {
    super.onStart();
    if (FRIEND_PICKER.equals(getIntent().getData())) {
        try {
            friendPickerFragment.loadData(false);
        } catch (Exception ex) {
            onError(ex);
        }
    }
}

@Override
protected void onResume() {
    Log.i("", "location test onResume");
    super.onResume();
    MyApp.getInstance().pref.setIsBackground(this, false);
    MyApp.getInstance().startLocationClient();
}

@Override
protected void onPause() {
    Log.i("", "location test onPause");
    super.onPause();
    MyApp.getInstance().pref.setIsBackground(this, true);
}
}

Now I looked over this fragment, do not know if I have to add something or save something from the fragment on "onDoneButtonClicked"? or what exactly, because my main activity does return null as data..


Solution

  • forgot to call this in the finishActivty:

    if (FRIEND_PICKER.equals(getIntent().getData())) {
            if (friendPickerFragment != null) {
                MyApp.getInstance().setSelectedUsers(friendPickerFragment.getSelection());
            }
        }
    

    Now I can get from my Application the list of selected users.

    About my edit, after this i found out that with Graph2.0 you cannot get a list of your whole friendlist. You can only get back the info of friends that also liked the app. It is possible to invite friends to like an app but only if you set it as a Game from the FB developers page