Android - Facebook SDK 3, login and message on the wall with one button

Below is the code from the official textbook. I use ActionBarSherlock, so my activity extends SherlockFragmentActivity, it works if I use them once each (login and message), but it cannot work if I want to log in and send with just one button.

Class variable:

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization"; private boolean pendingPublishReauthorization = false; 

Action bar button:

  case R.id.askOnFacebook: { final Activity tmpAct = this; // start Facebook Login Session.openActiveSession(this, true, new Session.StatusCallback() { // callback when session changes state @Override public void call(Session session, SessionState state, Exception exception) { if (session.isOpened()) { // make request to the /me API Request.newMeRequest(session, new Request.GraphUserCallback() { // callback after Graph API response with user object @Override public void onCompleted(GraphUser user, Response response) { if (user != null) { Toast.makeText(getApplicationContext(), "utente " + user.getName(), Toast.LENGTH_LONG).show(); publishStory(tmpAct); } } }); } } }); } 

other methods:

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); } private void publishStory(final Activity activity) { Session session = Session.getActiveSession(); if (session != null){ // Check for publish permissions List<String> permissions = session.getPermissions(); if (!isSubsetOf(PERMISSIONS, permissions)) { pendingPublishReauthorization = true; Session.NewPermissionsRequest newPermissionsRequest = new Session .NewPermissionsRequest(this, PERMISSIONS); session.requestNewPublishPermissions(newPermissionsRequest); return; } Bundle postParams = new Bundle(); postParams.putString("name", "nam"); postParams.putString("caption", "capProva"); postParams.putString("description", "desAsd"); postParams.putString("link", "https://developers.facebook.com/android"); postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); Request.Callback callback= new Request.Callback() { public void onCompleted(Response response) { JSONObject graphResponse = response .getGraphObject() .getInnerJSONObject(); String postId = null; try { postId = graphResponse.getString("id"); } catch (JSONException e) { Log.i(activity.toString(), "JSON error "+ e.getMessage()); } FacebookRequestError error = response.getError(); if (error != null) { Toast.makeText(activity .getApplicationContext(), "ERROR: " + error.getErrorMessage(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(activity .getApplicationContext(), "POSTED: " + postId, Toast.LENGTH_LONG).show(); } } }; Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback); RequestAsyncTask task = new RequestAsyncTask(request); task.execute(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization); //XXX !!!!!!! I removed the line below because in the tutorial onCreate method it not initialized //uiHelper.onSaveInstanceState(outState); } private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) { for (String string : subset) { if (!superset.contains(string)) { return false; } } return true; } 

inside onCreate:

  if (savedInstanceState != null) { pendingPublishReauthorization = savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false); } 

I noticed that in debug mode it never jumps from onCompleted, so the publish method is never called. If I transfer the call to publishStory after Session.openActiveSession, it does not work well and I get some failure (I suppose because before it needs to be logged in and after I need to click the button again to send a message to facebook wall).

How could everything work with the click of a button?

0
source share

All Articles