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