In my application on the start page, I ask the user to authenticate via Facebook, then I request some permissions and get some information:
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
fb = new FacebookMain();
I can get all this information, but going to the next page, I want to give a button in my list, and from there the user can post on the friends wall. I followed the HelloFacebook example and it works like a charm, however, in my case, when I try to implement it in a fragment, it doesn’t work as intended, I don’t want the user to log in every time he wants to publish ( I use additional permission here - for publication). Should I implement all life events here in this fragment? Is there any other or recommended approach to this?
I am currently doing:
declaration of my class:
public class FragmentTab1 extends Fragment {
Class Level Variables:
String FACEBOOK_ID;
String IMAGE_CONTENT;
EditText SEARCH;
private static final String PERMISSION = "publish_actions";
Session session;
private final String PENDING_ACTION_BUNDLE_KEY = "com.exa.digitalrem:PendingAction";
private PendingAction pendingAction = PendingAction.NONE;
private GraphUser user1;
private UiLifecycleHelper uiHelper;
Features related to facebook:
private PendingAction pendingAction = PendingAction.NONE;
private GraphUser user1;
private enum PendingAction {
NONE, POST_PHOTO, POST_STATUS_UPDATE
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
public static boolean isActive() {
Session session = Session.getActiveSession();
if (session == null) {
return false;
}
return session.isOpened();
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (pendingAction != PendingAction.NONE && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(getActivity()) .setTitle("cancelled").setMessage("NotGranted").setPositiveButton("Ok", null).show();
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
updateUI();
}
private void updateUI() {
Session session = Session.getActiveSession();
boolean enableButtons = (session != null && session.isOpened());
if (enableButtons && user1 != null) {
} else {
}
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
pendingAction = PendingAction.NONE;
}
In onCreate:
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
if (savedInstanceState != null) {
name = savedInstanceState.getString(PENDING_ACTION_BUNDLE_KEY);
pendingAction = PendingAction.valueOf(name);
}
Session.openActiveSessionFromCache(getActivity());
This is my facebook post method, I call it with the click of a button, which is in the list:
public void postFB(String id) {
System.out.println("in fb");
if (isNetworkConnected()) {
Session session = Session.getActiveSession();
if (session != null) {
System.out.println("session not null");
if (hasPublishPermission()) {
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
getActivity(), Session.openActiveSessionFromCache(getActivity()),
params)).setOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
}
}).build();
feedDialog.show();
} else if (session.isOpened()) {
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(
getActivity(), PERMISSION));
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
getActivity(), Session.getActiveSession(),
params)).setOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
}
}).build();
feedDialog.show();
}
}else if(session == null){
System.out.println("login");
}
} else {
Toast.makeText(getActivity(),
"Please check your internet connection", Toast.LENGTH_LONG)
.show();
}
}
, , , ? , , ? ""?