Question

I'm trying create a simple thing when user clicks the button then facebook login process should work. I did this with the deprecated methods and it FC the app before logging. In FB SDK 3.5 i have used session class to create that login by following many tutorials and they are also going FC at start on the activity. Here is my code,

public class MainActivity extends Activity implements OnClickListener {

TextView username;
String APP_ID;
Button btn_login;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    APP_ID = getString(R.string.app_id);
    btn_login = (Button)findViewById(R.id.login);
    btn_login.setOnClickListener(this);         

}       
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onClick(View view) {    

    Session.openActiveSession(this, true, new Session.StatusCallback(){
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {

        }           
    });
}
}

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.facebookapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

    <activity android:name="com.facebook.LoginActivity" />

<meta-data
    android:name="com.facebook.sdk.ApplicationId"
    android:value="@string/app_id" />

</application>

Is this way correct to have only a login function. If wrong please help me to how to do it on Facebook SDK 3.5 without native LoginButton system. Hope your kind answers.

Was it helpful?

Solution

Take a look at this method:

 private boolean ensureOpenSession() {
    if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) 
    {
        Session.openActiveSession(this, true, new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) 
            {
                onSessionStateChanged(session, state, exception);
            }
        });
        return false;
    }
    else
    {
        onSessionStateChanged(Session.getActiveSession(), Session.getActiveSession().getState(), null);
    }
    return true;
}

and will have to create the onSessionStateChanged method:

 private void onSessionStateChanged(Session session, SessionState state, Exception exception)
 {}

In which you can specify what happens when Session state changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top