3

My friend and I are trying to login via facebook. We took code from facebook developer page: https://developers.facebook.com/docs/android/login-with-facebook

The problem is how to call another activity right after authorization succeed and remove logout button. We've been reading other posts but there is no solution that helped us.

andzaOs
  • 155
  • 1
  • 2
  • 12

3 Answers3

4

I'm going to show this solution because maybe it helps in the future someone else who is struggling like me with the same problem.

In order to remove logout/login button and to prevent their appearance when you click the back button or totally close the activity (but the user is still logged in because their credentials has been saved).

To solve this problem you have to call the finish() function and to close the LoginActivity.

For example, consider LoginActivity as the launcher of my application. It extends Activity, hence it has onCreate() and onActivityResult():

public class LoginActivity extends HelpBaseActivity {
   private LoginButton loginButton;
   private CallbackManager callbackManager;
   private AccessToken accessToken;
   private AccessTokenTracker accessTokenTracker;

   //Intent Actions
   private static final String HOME_ACTIVITIES = "com.example.mydomain.myappname.HOME_ACTIVITY";
   // Request Code
   private static final int HOME_ACTIVITIES_REQUEST_CODE = 10;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      FacebookSdk.sdkInitialize(this);
      setContentView(R.layout.activity_login);
      loginButton = (LoginButton) findViewById(R.id.login_button);
      callbackManager = CallbackManager.Factory.create();
      // Callback registration
      loginButton.registerCallback(callbackManager, new   FacebookCallback<LoginResult>() {
          @Override
          public void onSuccess(LoginResult loginResult) {
              Log.d("LOGIN_SUCCESS", "Success");
              loginButton.setVisibility(View.INVISIBLE); //<- IMPORTANT
              Intent intent = new Intent(HOME_ACTIVITIES);
              startActivity(intent);
              finish();//<- IMPORTANT
          }
          @Override
          public void onCancel() {
              Log.d("LOGIN_CANCEL", "Cancel");
          }

          @Override
          public void onError(FacebookException exception) {
              Log.d("LOGIN_ERROR", "Error");
          }
      });

      accessTokenTracker = new AccessTokenTracker() {
          @Override
          protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {
            // Set the access token using
            // currentAccessToken when it's loaded or set.
          }
      };
      // If the access token is available already assign it.
      accessToken = AccessToken.getCurrentAccessToken();
      // If already logged in show the home view
      if (accessToken != null) {//<- IMPORTANT
          Intent intent = new Intent(HOME_ACTIVITIES);
          startActivity(intent);
          finish();//<- IMPORTANT
      }
  }

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

}

If yout want to control your "HomeActivity" then you call the activity with startActivityForResult() (and not startActivity()) as:

Intent intent = new Intent(HOME_ACTIVITIES);
startActivityForResult(intent, HOME_ACTIVITIES_REQUEST_CODE);

and then on onActivityResult() you check like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
      if (requestCode == HOME_ACTIVITIES_REQUEST_CODE) {
          finish();
      }
}

I am new on Android Programming, so I am sure there could be many other solutions more brilliant than this, but I didn't find useful for me those provided in the internet until now, so I wanted to show the one that worked for me.

Totoro
  • 156
  • 2
  • 13
3

Have you implemented this?

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

If yes, you can easily guess that the code in the if(state.isOpened()) block is executed every time that the user logs in. So, add the following line:

startActivity(new Intent(MainActicity.this, NewActivity.class));

EDIT:

facebook.isSessionValid() returns true if user is logged in, false if not. This method seems to be deprecated, however it should continue to work. Source

So, in the onCreate() method add:

if (facebook.isSessionValid())
   startActivity(new Intent(MainActicity.this, NewActivity.class));
Community
  • 1
  • 1
babis
  • 228
  • 1
  • 9
  • Thank you for your answer. Another problem is when we push button back and leave app, after we open it again log out button shows up instead of new activity. Is there any way to fix it? – andzaOs Apr 05 '14 at 20:18
  • 1
    I have edited my answer, check the edit for details. I would appreciate it if you selected my answer as correct, thanks. – babis Apr 06 '14 at 06:53
0

you can also start a new activity inside the onSuccess method provided @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_main);

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                }

                @Override
                public void onCancel() {
                    // App code
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                }
            });
Austine Gwa
  • 804
  • 1
  • 9
  • 18