android - 登录 : after going to another activity app forgot Facebook picture, 和名称文本

标签 android facebook facebook-android-sdk facebook-sdk-4.0

我在 Android 应用程序中使用 Facebook SDK 时遇到两个问题,无法解决。

  1. (已解决)进入其他 Activity 后,或重新打开应用程序。它不记得个人资料图片和姓名(名字和姓氏)。

  2. 如果您选择注销,它仍会显示您的姓名和图片。

如果有人可以帮助我,我将非常高兴。请提供代码 - 不要说只是这样做。谢谢你!

public class Profile extends Activity {
/**
 * Called when the activity is first created.
 */

WebView web;
ArrayList<NavigationDrawerItem> listItems;
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
ListView list;

private CallbackManager callbackManager;
private ProfilePictureView profilePictureView;
private TextView name;


       @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_profile);
        callbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile", "email", "user_friends");
        final ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profilepic);
        final TextView name = (TextView) findViewById(R.id.name);

        final SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);
        //After referencing your Views, add this.
        String nameStr = sharedPrefs.getString("name", null);
        String idStr = sharedPrefs.getString("id", null);
        if(nameStr != null)
        {
            name.setText(nameStr);
        }
        if(idStr != null)
        {
            profilePictureView.setProfileId(idStr);
        }
        //.. Do the same for other profile data


        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken accessToken = loginResult.getAccessToken();
                com.facebook.Profile profile = com.facebook.Profile.getCurrentProfile();
                profilePictureView.getProfileId();
                profilePictureView.setProfileId(profile.getId());
                name.setText(profile.getName());

                SharedPreferences.Editor editor = sharedPrefs.edit();
                editor.putString("name", profile.getName());
                editor.putString("id", profile.getId());
                //.. Do the same for other profile data
                editor.commit();

            }



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


            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.i("Error", "Error");
            }

        });


    }

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

最佳答案

将详细信息存储在 SharedPreferences 中并检索它们。

将其添加到您的onCreate()中。

SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);

在回调的 onSuccess() 中添加,

Editor editor = sharedPrefs.edit();
editor.putString("name", profile.getName());
editor.putString("id", profile.getId());
//.. Do the same for other profile data
editor.commit();

再次在您的onCreate()中,

SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);
//After referencing your Views, add this.
String nameStr = sharedPrefs.getString("name", null);
String idStr = sharedPrefs.getString("id", null);
AccessToken token = AccessToken.getCurrentAccessToken();
if(token != null)
{
    if(nameStr != null)
    {
       name.setText(nameStr);
    }
    if(idStr != null)
    {
       profilePictureView.setProfileId(id);
    }
]
//.. Do the same for other profile data

关于android - 登录 : after going to another activity app forgot Facebook picture, 和名称文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650068/

相关文章:

android - Telegram API 中的 messages.getMessages 方法是否也支持 Telegram channel ?

ios - 使用 FBSessionLoginBehaviorForcingWebView 登录 Facebook

iOS 在 UIView 中滑动到其他 UIView 之上

android - 从 facebook 墙到 android 应用程序的新闻提要

android - 如何避免在 Android Facebook SDK 中显示已授权应用对话框

android - Facebook session 单例使用

android - 设置 android :text from strings. xml

android - 将 Listview 移动到 View android 的底部

android - AOSP/Android 源码索引工具

ios - 关闭应用程序后 Facebook session 无效