I want to display the username on the user profile that the user inputs on the first activity. I am using SharedPreferences, but the following code does not display the username on the other activity.
This is the code on the activity where the user inputs the username:
public class MainActivity extends AppCompatActivity {
EditText userName, password;
Button b1;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName=findViewById(R.id.usernameText);
password=findViewById(R.id.passwordText);
b1=findViewById(R.id.button);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(binding.usernameText.length()==0 && binding.passwordText.length()==0){
binding.fillInTheBlanks.setText("Please, fill in the blanks.");
binding.passwordConstraint.setText("");
}
else{
SharedPreferences sp = getSharedPreferences("database", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("Username", userName.getText().toString());
editor.apply();
binding.fillInTheBlanks.setText("");
binding.passwordConstraint.setText("");
startActivity(new Intent(v.getContext(), YourGenderActivity.class));
}
}
});
}
}
And this is the code where I want to read and display saved information from SharedPreferences:
public class MyProfileActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_profile_activity);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String userstring = sp.getString("Username",null);
TextView username = findViewById(R.id.userName);
username.setText(userstring); //this does not do anything on the activity
}
public void homeActivity(View v){
Intent intent = new Intent(v.getContext(), HomeActivity.class);
startActivity(intent);
}
}
If there is another way to achieve this, any help is welcome.
3
Answers
If you just want an implementation of
SharedPreferences
, you can do it like that:To write to the
SharedPreferences
:To retrieve information:
Another problem is the implementation of context is
new Intent(v.getContext())
it could be as simple asstartActivity(this, YourGenderActivity.class));
This code snippet just uses
this
for the context then it is similar with next profile contextthis
NOTES:
You are using 2 different
SharedPreferences
objects. You need to get the same one usinggetSharedPreferences("database", Context.MODE_PRIVATE)
in both activities. By usingPreferenceManager.getDefaultSharedPreferences(context)
, you’re using aSharedPreferences
object that is associated to your package name (nothing wrong with using that, but I prefer to be explicit).You have specified a name for your shared preference named ‘database’
In your second activity you must exactly get that name again so
use this in your second activity:
In SharedPreference you can have multiple names to arrange your data for example you can specify a name for all data related to theme,database and…
to get android default name for sharedPreference use :