1

How to make this below code to remember the login did by the user in android app. I need the app to remember some data's like username and profile picture,but right now the app loses the data's like username and profile picture when I restart(exit and reopen) the app

public class SelfTrail extends AppCompatActivity {

private LoginButton btnLogin;
private TextView facebookName;
private CallbackManager callbackManager;
private ProfilePictureView profilePictureView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());

    setContentView(R.layout.activity_self_trail);

    btnLogin = (LoginButton)findViewById(R.id.login_button);
    facebookName = (TextView)findViewById(R.id.name);
    profilePictureView = (ProfilePictureView)findViewById(R.id.image);


    btnLogin.setReadPermissions(Arrays.asList("public_profile, email"));
    callbackManager = CallbackManager.Factory.create();
    btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {

                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("Main", response.toString());
                            setProfileToView(object);
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException exception) {

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

private void setProfileToView(JSONObject jsonObject) {
    try {
        facebookName.setText(jsonObject.getString("name"));

        profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
        profilePictureView.setProfileId(jsonObject.getString("id"));

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

feel free to correct the code,Thank you in advance

Eggsy
  • 133
  • 2
  • 6
  • 15
  • Did u try using preferences or you can also use onSaveInstanceState . Just take a look and see if it works for u. – Amit Kumar Apr 02 '16 at 12:03
  • @AmitKumar I am sooo frustrated man I dont no where and how to implement,been trying since u said – Eggsy Apr 03 '16 at 07:26
  • http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please - Did u try some like this in the answers for onsaveinstancestate ? – Amit Kumar Apr 03 '16 at 17:39

1 Answers1

0

I'd recommend you save the profile picture locally and remember the string for the location of that picture, along with the username and password. this is a simple storage class i started with to store info like username name and password

public class UserLocalStore {
    public int x = 0;
    public static final String SP_NAME = "userDetails";
    SharedPreferences userLocalDatabase;
    public int currentID;
    public  UserLocalStore(Context context){
        userLocalDatabase = context.getSharedPreferences(SP_NAME, 0);

    }
    public void storeUserData(User user){
        SharedPreferences.Editor spEditor = userLocalDatabase.edit();
        //spEditor.putString("ID", Integer.toString(x));
        spEditor.putString(Integer.toString(x) +" name", user.name);
        spEditor.putString(Integer.toString(x) +" username", user.username);
        spEditor.putString(Integer.toString(x) +" password", user.password);
        x++;
        spEditor.commit();

    }
    public int getByUsername(String findusername){
        int x = 0;

        while(x < 10){
            String test = userLocalDatabase.getString(Integer.toString(x)+ " username", "");
        if(test.equals(findusername)){

            String name = userLocalDatabase.getString(Integer.toString(x)+" name", "");
            String username = userLocalDatabase.getString(Integer.toString(x)+" username", "");
            String password = userLocalDatabase.getString(Integer.toString(x)+" password", "");
            User foundUser = new User(name,username,password);
            return x;
        }
            x++;
        }


        return 0;
    }

    public ArrayList<User> getAllUsers(){
        int x = 0;
        ArrayList<User> allUsers = new ArrayList<>();
        User tempuser = new User("","","");
        while(x < 10) {
            if(userLocalDatabase.getString(Integer.toString(x) + " name", "").equals("")) {
            return allUsers;
            }
            String name = userLocalDatabase.getString(Integer.toString(x) + " name", "");
            String username = userLocalDatabase.getString(Integer.toString(x) + " username", "");
            String password = userLocalDatabase.getString(Integer.toString(x) + " password", "");
            tempuser.name = name;
            tempuser.username = username;
            tempuser.password = password;
           allUsers.add(tempuser);
            x++;
        } return allUsers;

    }

    public User getLoggedInUser(int ID){
        String name = userLocalDatabase.getString(Integer.toString(ID)+" name", "");
        String username = userLocalDatabase.getString(Integer.toString(ID)+" username", "");
        String password = userLocalDatabase.getString(Integer.toString(ID)+" password", "");

        User storedUser = new User(name,username,password);
        return  storedUser;
    }
    public User getStoredPreferences(){
        String name = userLocalDatabase.getString("name", "");
        String username = userLocalDatabase.getString("username", "");
        String password = userLocalDatabase.getString("password", "");

        User storedUser = new User(name,username,password);
        return  storedUser;
    }


    public void setUserLoggedIn(int loggedIn){
        currentID = loggedIn;
      }

    public int getUserLoggedIn() {
        return currentID;
    }

    public void clearUserData(){
        SharedPreferences.Editor spEditor = userLocalDatabase.edit();
        spEditor.clear();
        spEditor.commit();


    } }

Then you can call it from another class like this

User addthisuser = new User(name, username, password);

userLocalStore.storeUserData(addthisuser);

Jube
  • 184
  • 2
  • 15