In my application on the start page I ask the user to authenticate via Facebook, then I request for some permissions and fetch 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 am able to get all this information, but moving to my next page I want to give a button on my listview and from there a user can post on a friends wall. I followed the HelloFacebook sample and it works like a charm, however in my case when I try to implement it in a fragment it does not work as intended, I dont want the user to login every time he wants to post (I am using an additional permission here -- to post) DO I have to implement all the lifecycly events here, in this fragment? Is there any other or recommended approach to this?
Currently what I am doing is:
my class declaration:
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;
Functions 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) {
// profilePictureView.setProfileId(user.getId());
// greeting.setText(getString(R.string.app_name, user.getFirstName()));
} else {
// profilePictureView.setProfileId(null);
// greeting.setText(null);
}
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
// These actions may re-set pendingAction if they are still pending, but
// we assume they
// will succeed.
pendingAction = PendingAction.NONE;
}
In the 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 this on a button click, which is in a listview:
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()) {
//do something
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
getActivity(), Session.openActiveSessionFromCache(getActivity()),
params)).setOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
// frag3.setFbId(null);
// ---------------------------- got to put
// check here
// onBackPressed();
}
}).build();
feedDialog.show();
} else if (session.isOpened()) {
// We need to get new permissions, then complete the action
// when we get called back.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(
getActivity(), PERMISSION));
// do something
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
getActivity(), Session.getActiveSession(),
params)).setOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
// frag3.setFbId(null);
// ---------------------------- got to put
// check here
//onBackPressed();
}
}).build();
feedDialog.show();
}
}else if(session == null){
System.out.println("login");
}
} else {
Toast.makeText(getActivity(),
"Please check your internet connection", Toast.LENGTH_LONG)
.show();
}
}
This however does not seem to me a correct approach, is there any better way? Also how do I detect if the user session is expired and prompt the user to login again? The Docs dont seem to reveal the internal functioning of the Login button?
Do not start a new session .... change it in the source code
Add below lines in AndroidManifest.xml under tag
Also Add facebook app id in string.xml
Declare the private members
See your log cat with tag "json" you will have a facebook object