0

I am building an app to upload images to my company server.

Now my problem is that my SharedPreferences for the login activity are not saving the data is passed to other activities using SharedPreferences perfectly, but only if you re-enter the login details again (because it disappears out of the edit textboxes every time you go out of the login activity) and press the login button.

Here is my code for the login activity.

 public class LoginActivity extends AppCompatActivity {

    EditText username, password1, clientID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.emailtext);
        password1 = findViewById(R.id.pwdtext);
        clientID = findViewById(R.id.clientidtxt);
        SharedPreferences.Editor editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit();
        editor.putString("name", username.getText().toString());
        editor.putString("password", password1.getText().toString());
        editor.putString("id", clientID.getText().toString());
        editor.apply();

        Button button3=findViewById(R.id.button);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit();
                editor.putString("name", username.getText().toString());
                editor.putString("password", password1.getText().toString());
                editor.putString("id", clientID.getText().toString());
                editor.apply();

                Intent activity2Intent=new Intent(getApplicationContext(),MainActivity.class);

                startActivity(activity2Intent);
            }
        });
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Ruben Meiring
  • 333
  • 2
  • 21
  • 2
    I'm not sure if I'm following your description correctly, but why are you immediately saving those `EditText`s' texts in `onCreate()`? Unless you have text set on the `` elements in the layout, they're going to be empty. Don't you want to load the values from `SharedPreferences` into those `EditText`s instead? – Mike M. Aug 19 '19 at 07:08
  • Possible duplicate of [Android shared preferences not saving](https://stackoverflow.com/questions/9677719/android-shared-preferences-not-saving) – Swaminathan V Aug 19 '19 at 07:08
  • @RaguSwaminathan I tried the solution there and it didn't help or work FYI i have only been coding for 2 weeks so I might know how to implement that correctly though – Ruben Meiring Aug 19 '19 at 07:11
  • @MikeM. I have only been coding for 2 weeks so I don't know, just thought it was the way it should be to work, Yes I want to load those values to the edit texts and I dont know how, any assistance would be awesome – Ruben Meiring Aug 19 '19 at 07:13
  • Yeah, it seems like you want to retrieve, there, instead of saving. Looks like you're getting some answers that should help you to do that. – Mike M. Aug 19 '19 at 07:18

2 Answers2

1

I am not sure how you are confirming that the values are not set in your SharedPreference? I cannot see anything wrong with the saving process. Its all just fine.

However, I think you are not populating the values from your SharedPreferences in your username and password views in the onCreate function. I hope this is something you are looking for.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // view references
    username = findViewById(R.id.emailtext);
    password1 = findViewById(R.id.pwdtext);
    clientID = findViewById(R.id.clientidtxt);

    SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);

    // Set the views here with the values found from your SharedPreferences. 
    // In the first time, there is no value stored, hence the default empty string will be put.
    username.setText(prefs.getString("name", ""));
    password1.setText(prefs.getString("password", ""));
    clientID.setText(prefs.getString("id", ""));

    Button button3=findViewById(R.id.button);
    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            SharedPreferences.Editor editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit();
            editor.putString("name", username.getText().toString());
            editor.putString("password", password1.getText().toString());
            editor.putString("id", clientID.getText().toString());
            editor.apply();

            Intent activity2Intent=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(activity2Intent);
        }
    }
}

Hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Thanks dude that was what I was looking for, it works perfectly – Ruben Meiring Aug 19 '19 at 07:22
  • Great to know that I could help. You are welcome. :) – Reaz Murshed Aug 19 '19 at 07:23
  • Sorry dude accidentally click the wrong answer as salman posted the same thing BUT only after me requesting his answer was wrong, I don't know what the rules are here for that but you gave the correct answer first, but he POSTED the answer first so I don;t know what the rules say about this – Ruben Meiring Aug 19 '19 at 07:32
1

Remove the first edit of SharedPreferences from your onCreate method. It will collect the empty values from the EditText fields and save it in shared preference. If you want to populate the fields when the activity starts use getString method and set the value in the EditText filed. As in:

SharedPreferences prefs = getSharedPreferences(MyPrefs, MODE_PRIVATE);
String name = prefs.getString("name", "John Doe");
username.setText(name);

Your code should be like this:

public class LoginActivity extends AppCompatActivity {

     EditText username, password1, clientID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username=findViewById(R.id.emailtext);
        password1=findViewById(R.id.pwdtext);
        clientID=findViewById(R.id.clientidtxt);
        Button button3=findViewById(R.id.button);

        SharedPreferences prefs = getSharedPreferences(MyPrefs, MODE_PRIVATE);
        String name = prefs.getString("name", "John Doe");
        username.setText(name); // set the value

        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit();
                editor.putString("name", username.getText().toString());
                editor.putString("password", password1.getText().toString());
                editor.putString("id", clientID.getText().toString());

                editor.apply();

                Intent activity2Intent=new Intent(getApplicationContext(),MainActivity.class);

                startActivity(activity2Intent);
            }
        });
salmanwahed
  • 9,450
  • 7
  • 32
  • 55