Note that there are some explanatory texts on larger screens.

plurals
  1. POFacebook SDK 3.0 callback fires twice
    primarykey
    data
    text
    <p>I have the following activity and I am using Facebook's LoginButton. onSessionStateChange is being called multiple times. I have an asynctask that I want to run after a successful login that also opens a new activity once complete. Right now this starts multiple async tasks. How can I find the final state so it will not fire twice? Ive looked through all of the examples and Facebook says that session.isOpened() should work, but its still firing multiple times. </p> <p>UPDATE:</p> <p>After removing the session code from onResume it only gets called once but according to <a href="https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/#step3" rel="noreferrer">https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/#step3</a> I need that code in OnResume for certain situations.</p> <pre><code>public class LoginActivity extends SherlockActivity { private static final String TAG = "LoginActivity"; private Context context; private int statusCode; private String emailAddress = null; private String password = null; private GraphUser fbUser; private UiLifecycleHelper uiHelper; private Session.StatusCallback callback = new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { onSessionStateChange(session, state, exception); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); context = getApplicationContext(); uiHelper = new UiLifecycleHelper(this, callback); uiHelper.onCreate(savedInstanceState); final LoginButton fbBtn = (LoginButton) findViewById(R.id.facebook_login); fbBtn.setReadPermissions(Arrays.asList("basic_info", "email")); /* * fbBtn.setOnClickListener(new View.OnClickListener() { * * @Override public void onClick(View v) { * * Intent intent = new Intent(context, MainActivity.class); * startActivity(intent); * * finish(); * * } }); */ } @Override protected void onPause() { super.onPause(); uiHelper.onPause(); } @Override protected void onDestroy() { super.onDestroy(); uiHelper.onDestroy(); } @Override protected void onResume() { super.onResume(); // For scenarios where the main activity is launched and user // session is not null, the session state change notification // may not be triggered. Trigger it if it's open/closed. Session session = Session.getActiveSession(); if (session != null &amp;&amp; (session.isOpened() || session.isClosed())) { onSessionStateChange(session, session.getState(), null); } uiHelper.onResume(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); uiHelper.onSaveInstanceState(outState); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d("FbLogin", "Result Code is - " + resultCode +""); uiHelper.onActivityResult(requestCode, resultCode, data); } private void onSessionStateChange(Session session, SessionState state, Exception exception) { if (session != null &amp;&amp; session.isOpened()) { Log.i(TAG, "Logged in..."); Log.i(TAG, "Access Token" + session.getAccessToken()); if (state == SessionState.OPENED) { Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { if (user != null) { Log.i(TAG, "User ID " + user.getId()); Log.i(TAG, "Email " + user.asMap().get("email")); fbUser = user; //FbRegisterTask fbReg = new FbRegisterTask(LoginActivity.this, user); //fbReg.execute(); //finish(); } } }); } } else if (session.isClosed()) { Log.i(TAG, "Logged out..."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload