Search code examples
androidfacebookfacebook-android-sdkfacebook-sdk-4.0

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


i have two issues i can't figure out using Facebook SDK inside my Android app.

  1. (SOLVED) After going to another activity, or re-open the app. It doesn't remember the profile picture, and the name(First, and Last name).

  2. If you choose to log out it still display your name, and picture.

I would be very glad if someone could assist me with this. Please provide code - not say just do that. Thank you!

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);
    }
}

Solution

  • Store the details in SharedPreferences and retrieve them.

    Add this to your onCreate().

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

    In the onSuccess() of your callback add,

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

    And in your onCreate() again,

    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