0

I am new to android development. I am trying to implement login through facebook using facebook SDK 4.0.0. Login is successful but it stays in the same activity for a second, the button changes to Logout and then the second activity appears. I want it to directly move to second activity. I searched for it but I couldn't figure it out.

(The viewflipper is calling next layout which is just a progress bar)

Following is my code.

public class MainActivity extends ActionBarActivity {


private String name;
private String lname;
private LoginButton loginButton;
private ProfileTracker  mProfileTracker;
private CallbackManager mCallBackManager;
private AccessTokenTracker mAccessTracker;
SharedPreferences sp;
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken atoken = loginResult.getAccessToken();
        ViewFlipper vp = (ViewFlipper) findViewById(R.id.flip);
        vp.showNext();
    }
    @Override
    public void onCancel() {
    }
    @Override
    public void onError(FacebookException e) {
    }
};;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sp = getPreferences(MODE_PRIVATE);
    if (sp.contains("username")) {
        Intent i = new Intent(this, userSelect.class);
        i.putExtra("username", sp.getString("username", "noUser"));
        i.putExtra("lname", sp.getString("lastname", "nomail"));
        startActivity(i);
    }

    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions("email");
    mCallBackManager = CallbackManager.Factory.create();
    loginButton.registerCallback(mCallBackManager, mCallBack);
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);



    if (sp.contains("username")) {
        Intent i = new Intent(this, userSelect.class);
        i.putExtra("username", sp.getString("username", "noUser"));
        i.putExtra("lname", sp.getString("lastname", "nomail"));
        startActivity(i);

    }
    mCallBackManager.onActivityResult(requestCode, resultCode, data);
    mAccessTracker = new AccessTokenTracker() {
       @Override
       protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
       }
    };
    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
            if ( newProfile != null)
            {

                name = newProfile.getFirstName();
                lname = newProfile.getLastName();
                Intent intent = new Intent(MainActivity.this, userSelect.class);
                Editor editor = sp.edit();
                editor.putString("username", name);
                editor.putString("lastname",lname );
                editor.commit();
                intent.putExtra("username", name);
                intent.putExtra("lname",lname);
                startActivity(intent);

            }
        }
    };
    mAccessTracker.startTracking();
    mProfileTracker.startTracking();
}

}

  • where you able to pass username to another activity after login.? b'cus I tried and it didn't end well.please help if needed Il share my code too – Eggsy May 28 '16 at 07:50

1 Answers1

0

You have to call new activity in your onSuccess method. By this your activity is called only after user logged in Successfully.

private FacebookCallback<LoginResult> mCallBack= new FacebookCallback<LoginResult>() {
 @Override
  public void onSuccess(LoginResult loginResult) {
   AccessToken accessToken = loginResult.getAccessToken();
   Profile profile = Profile.getCurrentProfile();
  
   //call new activity here.
}
Harvi Sirja
  • 2,472
  • 2
  • 18
  • 19
  • I did a edit in above code. Still the same thing. It displays mainactivity's layout for a second and then switches to other layout. – it_all_ends_here Aug 07 '15 at 07:42
  • you have do in this way because if you put it on button click than if user is not successfully login than also it allows to enter in second activity. – Harvi Sirja Aug 07 '15 at 08:54
  • But my problem is the lag I am facing. It does not directly goes to second activity even if I call new activity in onSuccess. for a second , mainactivity screen stays, then new activity is called. – it_all_ends_here Aug 07 '15 at 09:05
  • use custom button. Than this type of problem may not occurs. check here my answer. http://stackoverflow.com/questions/31327897/custom-facebook-login-button-android/31332928#31332928 – Harvi Sirja Aug 07 '15 at 09:20
  • was there a solution for this facebook lag problem? – joeabala Jan 18 '17 at 20:20