Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:24:37+00:00 2026-06-14T22:24:37+00:00

I followed this tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ to connect Facebook to my Android application. Instead of

  • 0

I followed this tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ to connect Facebook to my Android application. Instead of having many buttons, I have a button that will be used for both login and logout.

1) The first time I run the class, I could login and then logout successfully. And when I click on the same button to login again, the login page would not appear, however the Toast text “LOGGING IN” appear, which is after loginToFacebook() function in my if-else. Hence, I assume, it should have run the facebook login page like the first time I run the class. But the login page does not appear.

What did I do wrong? And what should I do?

2) And how do I display the username in String fbLoggedIn after logged in instead of the text “CONNECTED!!” ?

public class FacebookActivity extends Activity{

private static String APP_ID = "";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Button backButton;
private String name = "CONNECTED!!";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_share);

    facebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(facebook);

    RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
    fbButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!facebook.isSessionValid())
            {
            System.out.println("Not Connected. Clicked and Login.");    
            loginToFacebook();
            Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}

            else
            {
            System.out.println("Connected. Logged Out.");   
            logoutFromFacebook();
            Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
        }
    });     
}

public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {   

        facebook.authorize(this, new String[] { "email", "publish_stream" }, new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();   

                        TextView fbUser = (TextView) findViewById(R.id.fbUser);
                        fbUser.setVisibility(View.VISIBLE);
                        String fbLoggedIn = name;
                        fbUser.setText(fbLoggedIn); 
                        Toast.makeText(getApplicationContext(), "LOGGED IN AS " + name, Toast.LENGTH_LONG).show();         
                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error
                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
                    }

                });
    }
}

public void getProfileInformation() {
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Profile", response);
            String json = response;
            try {
                JSONObject profile = new JSONObject(json);
                // getting name of the user
                final String name = profile.getString("name");
                // getting email of the user
                final String email = profile.getString("email");

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                    }

                });

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onIOException(IOException e, Object state) { }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) { }

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) { }

        @Override
        public void onFacebookError(FacebookError e, Object state) { }
    });
}

//logout from Facebook
public void logoutFromFacebook() {
    mAsyncRunner.logout(this, new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Logout from Facebook", response);
            if (Boolean.parseBoolean(response) == true) {
                // User successfully Logged out
         }

        }

        @Override
        public void onIOException(IOException e, Object state) { }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) { }

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) { }

        @Override
        public void onFacebookError(FacebookError e, Object state) { }
    });
 }

Just to be clearer, I set my if-else like this (extracted from the complete codes above).

    RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
    fbButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!facebook.isSessionValid())
            {
            System.out.println("Not Connected. Clicked and Login.");    
            loginToFacebook();
            Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}

            else
            {
            System.out.println("Connected. Logged Out.");   
            logoutFromFacebook();
            Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
        }
    }); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:24:38+00:00Added an answer on June 14, 2026 at 10:24 pm

    This is the behavior that Facebook wants you to take with their API.Your login method checks to see if you have already signed in once and that the facebook session is active. If the session is active then it sets the properties of the facebook object and connects it behind the sences so that the login page does not show again.

    So you’re not doing anything wrong, it’s suppose to happen this way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a new facebook application deverloper. I followed this tutorial: http://www.adobe.com/devnet/facebook/articles/video_facebook_quick_start.html but I
I followed a tutorial located here: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ and so far everything worked fine with
I followed this tutorial here: http://www.dannyherran.com/2011/02/facebook-php-sdk-and-codeigniter-for-basic-user-authentication/ It works great but when I enable CSRF
Hi have followed this tutorial using Core plot framework http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application My project compiles, but
I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
I followed this tutorial on creating a simple rss reader... http://www.kieranmcgrady.com/blog/2012/4/25/tutorial-how-to-create-a-simple-rss-reader-for-ios.html The problem is
I followed this tutorial http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html I wrote the manifest File giving all the permissions
I followed this tutorial http://www.tugberkugurlu.com/archive/api-key-authorization-through-query-string-in-asp-net-web-api-authorizationfilterattribute to create custom Authorization filter. I have CarController with
I followed along with this tutorial ( http://www.bit-101.com/blog/?p=1861 ) and noticed that upon saving
I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.