Fabric for Android - log in user programatically and check for log in


I am trying to integrate Twitter using Fabric into my Android app, and I am having few issues, which I can’t find answer to in the Fabric documentation.

1) I need to be able to log-in the user programatically, meaning from a button I create, not the xml button provided in the docs. What code do I need?
2) If the user comes to my app again later, after he already has approved of logging in with Twitter, do I need to log in again programatically, if for example I want to post a tweet from code?
3) Can I check programatically using the twitter apis if the user is logged in?

If any of the above has any documentation, please point me to it

  1. You can create the button programmatically and performClick on it (and don’t forget the onActivityResult).
private TwitterLoginButton twitterLoginButton;

/* [... inside onCreate ... ] */
twitterLoginButton = new TwitterLoginButton();

yourButton.setOnClickListener( [...] ) {
  1. You don’t need to login again the user.

  2. You can get the current user session with:

TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();

If this var is not null you can use it in your requests.


Ok, here is what I do:

  1. I login to twitter using your code (or whatever, the xml one too), I click Authenticate, and then I am logged in successfully.
  2. I close the app
  3. I open it again, try the TwitterSession code you provided in debugging mode, and it is null
    Why is that?

EDIT: Actually I am getting null even without restarting the app


That is very helpful, but ridiculous that that’s necessary. Is this the officially recommended way of programmatically logging a user in without displaying Twitter’s official button?


Well, in fact if you want to use TwitterSession after login, you need to assign the result data to the session variable:

twitterLoginButton.setCallback(new Callback<TwitterSession>() {
    public void success(Result<TwitterSession> result) {
        twitterSession = result.data;

       // Do something with the session here

The other way to access session (getActiveSession) will be for successive times. Anyway, it’s rare that you can’t access to the session when close and reopen the app.

I think so, yes. When I tried for other way always fails for not passing the onActivityResult function to the button.


Actually, after playing around with the API again (specifically looking for the best way to get it to logout), I noticed some of the other classes in Twitter, specifically the AuthClient and the like, and have been able to successfully do all the things I need with only these.

I’ve gist’ed the helper class here, https://gist.github.com/seaders/e041b3a798db3201bdff which may be useful for you.


I hope not. because twitterLoginButton.performClick(); actually make a sound effect. Like you are pressing it. So when you trigger this onClick, you will here 2 clicks…

Please advise. I’ve managed to login with Twitter.login, but the callback is never triggered.


I have the same problem as you described at 2. How did you solve ?

closed #9