Unable to logout with Facebook4j

336 Views Asked by At

I'm using the Facebook4j library to integrate Facebook with my Android app. I successfully login following the example at https://github.com/roundrop/facebook4j-android-example, but I didn't found any documentation or example on how to logout.

Any suggestion?

1

There are 1 best solutions below

1
On

You can just add below code to your logout onclick

fbLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            clearApplicationData();
            FBHandler.getInstance(this).facebookLogout();
        }
    });

and create FBHandler class like below

public class FBHandler {
private static final String TAG = "FBHandler";
private static final String appKey = "Your App Key";
private static Facebook facebook = null;
private static FBHandler _instance = null;
private static Context context = null;
private static String[] permissions = null;

private FBHandler() {
}

public static FBHandler getInstance(Context ctx) {
    if (ctx == null) {
        Log.d(TAG,
                "Error: Context is null, hence returning from the FBHandler");
        return null;
    }

    context = ctx;
    if (_instance == null) {
        facebook = new Facebook(appKey);
        _instance = new FBHandler();
        permissions = new String[] { "publish_stream", "user_photos",
                "publish_checkins", "email", "publish_actions" };
    }

    return _instance;
}

public void doPrintoFbUserLogin(final Activity activity,
        final fbListner listner) {
    if (activity == null || listner == null) {
        Log.d(TAG, "Error: User Login activity/Listner is null");
        return;
    }

    facebook.authorize(activity, permissions, new DialogListener() {

        public void onComplete(Bundle values) {
            printoCommon.showToastMsg(activity,
                    "facebook Login is succeesfull");
            listner.onSuccess();

        }

        public void onFacebookError(FacebookError e) {
            printoCommon.showToastMsg(activity, e.getMessage());

        }

        public void onError(DialogError e) {

            printoCommon.showToastMsg(activity, e.getMessage());
        }

        public void onCancel() {
            printoCommon.showToastMsg(activity,
                    "Facebook Login is cancelled");

        }

    });
}

@SuppressWarnings("static-access")
public void uploadUserPhoto(String graphAction, final Activity activity,
        Bundle params, uploadListner listner) {

    AsyncFacebookRunner fbAsyncRun = new AsyncFacebookRunner(facebook);
    params.putString(facebook.TOKEN, facebook.getAccessToken());
    fbAsyncRun.request("me/" + graphAction, params, "POST",
            new photoUploadListener(activity), null);
}

public void facebookLogout() {

    AsyncFacebookRunner fbAsyncRun = new AsyncFacebookRunner(facebook);
    fbAsyncRun.logout(context, new AsyncFacebookRunner.RequestListener() {

        public void onMalformedURLException(MalformedURLException e,
                Object state) {
            printoCommon.showToastMsg((Activity) context, e.getMessage());
        }

        public void onIOException(IOException e, Object state) {
            printoCommon.showToastMsg((Activity) context, e.getMessage());
        }

        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
            printoCommon.showToastMsg((Activity) context, e.getMessage());
        }

        public void onFacebookError(FacebookError e, Object state) {
            printoCommon.showToastMsg((Activity) context, e.getMessage());
        }

        public void onComplete(String response, Object state) {
            printoCommon.showToastMsg((Activity) context,
                    "You have logged out from facebook Successfully");
            ((Activity) context).finish();
        }
    });
}
}