4

I've built an activity that uses this implementation (see the accepted answer) to post a status update on a user's facebook wall.

It works with no problem if the emulator/phone does not have the facebook app installed.

If the emulator/phone has the facebook app installed, the facebook app loads the login screen, but after trying to login, the facebook app just disappears bringing me back to my app.

Has anyone had this experience when the facebook app is installed?

My code:

public class AchievementActivity extends Activity implements DialogListener, OnClickListener{


private Facebook facebook;
Button facebookPostButton;
String defaultFacebookPost;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.achievements);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_layout);

    View achievementDivider = (View)findViewById(R.id.achievementDivider);
    int[] colors = {0, 0xff00ffff, 0};
    achievementDivider.setBackgroundDrawable(new GradientDrawable(Orientation.RIGHT_LEFT, colors));

    //get the title of the achievement from the intent that started this activity from the activity StatisticsActivity
    String achievementTitleString = getIntent().getStringExtra("title");

    String achievementTextToDisplay = getAchievementTextToDisplay(achievementTitleString);

    defaultFacebookPost = getDefaultPost(achievementTitleString);

    //ImageView achievementActivityAchievementBadgeImageView = (ImageView)findViewById(R.id.achievementActivityAchievementBadgeImageView);
    TextView achievementActivityBadgeTitleTextView = (TextView)findViewById(R.id.achievementActivityBadgeTitleTextView);
        achievementActivityBadgeTitleTextView.setText(achievementTitleString);

    TextView achievementActivityAchievementText = (TextView)findViewById(R.id.achievementActivityAchievementText);
        achievementActivityAchievementText.setText(achievementTextToDisplay);

    facebookPostButton = (Button)findViewById(R.id.facebookPostButton);
    facebookPostButton.setOnClickListener(this);


}

@Override
public void onComplete(Bundle values) {

    if (values.isEmpty())
    {
        Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT);

        return;
    }

    if (!values.containsKey("post_id"))
    {
        try
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", defaultFacebookPost);// the message to post to the wall
            facebook.dialog(AchievementActivity.this, "stream.publish", parameters, this);// "stream.publish" is an API call
        }
        catch (Exception e)
        {
            // TODO: handle exception
                System.out.println(e.getMessage());
        }
    }

    try 
    {
        facebook.logout(getApplicationContext());
    } 
    catch (MalformedURLException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onFacebookError(FacebookError error) 
{
    Toast.makeText(AchievementActivity.this, "onFacebookError", Toast.LENGTH_LONG); 
}

@Override
public void onError(DialogError e) 
{
    Toast.makeText(AchievementActivity.this, "onError", Toast.LENGTH_LONG); 
}

@Override
public void onCancel() 
{
    Toast.makeText(AchievementActivity.this, "onCancel", Toast.LENGTH_LONG);
}

@Override
public void onClick(View v)
{
    if (v == facebookPostButton)
    {
        facebook = new Facebook("my_facebook_api");
        // replace APP_API_ID with your own
        facebook.authorize(this, new String[] {"publish_stream", "read_stream", "offline_access"}, this); 
    }
}

private String getDefaultPost(String defaultTitleString) 
{
    //do some stuff here to get a string to post to wall

    return defaultPost;
}

private String getAchievementTextToDisplay(String achievementTitleString) {
    String achievementTextToDisplay = "DEFAULT";

    //do some stuff here to get text to display in the activity
            //this has nothing to do with the facebook post...

    return achievementTextToDisplay;
}

}

Logcat is telling me this:

05-11 13:03:34.076: INFO/ActivityManager(98): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) }
05-11 13:03:34.246: INFO/ActivityManager(98): Displayed activity com.facebook.katana/.ProxyAuth: 158 ms (total 158 ms)
05-11 13:03:35.166: DEBUG/dalvikvm(12390): GC_FOR_MALLOC freed 6729 objects / 418424 bytes in 44ms
05-11 13:03:35.166: DEBUG/webviewglue(12390): nativeDestroy view: 0x527e20
05-11 13:03:35.166: DEBUG/NativeCrypto(12390): Freeing OpenSSL session

EDIT : After having to install the android development platform on multiple machines over the past year and ALWAYS having an issue with Facebook after creating the new development environments, I have found that this simple answer can be the cause for a Facebook implementation working on one device and not another....

The secret Facebook API key (the one you list on developer.facebook.com) will be different for each version of your app that you package with a different certificate. For example, say you have two development machines. Since both of these machines will build your applications with a different certificates, you must ensure that you generate a Facebook API secret key for each machine and list them at developer.facebook.com.

Community
  • 1
  • 1
dell116
  • 5,835
  • 9
  • 52
  • 70
  • Do you have the latest FB app update installed? When I used the FB SDK in the past, I had some trouble, cause the FB app wasn't updated to the latest version (check in Market app, my apps, make sure there's no pending update available for the FB app). – Mathias Conradt May 12 '11 at 06:48
  • @Mathias Lin - I'm using the same version of Facebook that came with the faceboook SKD. They give you a compiled copy of the app when you get it from gitHub. – dell116 May 13 '11 at 15:57

3 Answers3

3

I have found a workaround but it is not the best so far.

facebook.authorize(activity, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, 
                new LoginDialogListener());

This will force that your app does not use the SSO if official facebook app is installed on the device. But there must be a better solution because there are apps out there which use sso with the official facebook app.

tobias
  • 2,322
  • 3
  • 33
  • 53
1

It is because when you logged in the facebook account then your login session is created in the device. You have to logout from the facebook after doing your task.

Amit Thaper
  • 2,117
  • 4
  • 26
  • 49
  • Thanks for the reply, but I still can't seem to get the facebook app to stay alive. It just disappears. I've edited my question to include the code for my activity. – dell116 May 11 '11 at 15:57
  • Please provide a more detailed answer! – tobias Feb 19 '12 at 10:14
0
private static final String[] PERMISSIONS =
        new String[] {"publish_stream", "read_stream", "offline_access"};  




@Override
public void onClick(View v)
{
    if (v == facebookPostButton)
    {
        mAsyncRunner    = new AsyncFacebookRunner(mFacebook);
                                   mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener());
                        mFacebook.authorize(FacebookImplement.this, PERMISSIONS, new AuthorizeListener());
    }
}



public class AuthorizeListener extends BaseDialogListener {

        public void onComplete(Bundle values) {

           Bundle params = new Bundle();
           params.putString("message"," Message");
       params.putString("description", "Wall Posting Description");        
        mAsyncRunner.request("me/feed", params, "POST",
                new UploadListener());         

        }

}

public class UploadListener extends BaseRequestListener {

        public void onComplete(final String response) {
              mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener());
}

}

 public class LogoutListener extends BaseRequestListener {


        public void onComplete(final String response) {         



        }
    }

May be this code helps you. If there is any problem then ask without any issue.

Amit Thaper
  • 2,117
  • 4
  • 26
  • 49
  • Thank you so much for providing me with your code, but I still get the same result. The facebook app opens, but closes right away and brings me right back to my app's main activity. Furthermore, I built a new project and used the facebook code from www.facebook/developers in my new project and once again the facebook app opens, and closes right away. This ONLY happends when the device has facebook installed, everything works fine when using webview. I'm at a complete loss here. – dell116 May 13 '11 at 17:46
  • You put me on the right track and your original answer of not properly logging in and out of the session is correct. After seeing your code above and looking at the examples on github I was finally able to get it working. – dell116 May 13 '11 at 20:35
  • I've got a similar problem. I don't quite follow your comments about properly logging in and out of the session... can you expand on that a bit? – Mick Byrne Jul 18 '11 at 06:27
  • when you logged in into the facebook account then your device created the session for that user id. when user comes again then that user id will be used as a login untill you logout the facebook account. – Amit Thaper Jul 18 '11 at 08:58
  • I don't follow this. The point of the Facebook API's Single Sign On feature is that the app user doesn't have to see the long in screen every time they use the app. That said, I've found their Android API to be very flakey. – sleep Nov 21 '11 at 23:29
  • I have the Same problem. But I dont get it to work like you. Can you provide me a more detailed explantation. – tobias Feb 18 '12 at 22:31