登录:转到另一个 activity 应用程序后忘记了 Facebook 图片和名称文本

Login: after going to another activity app forgot Facebook picture, and name text

我有两个问题无法解决,无法在我的 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