skip to Main Content

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


  1. If you just want an implementation of SharedPreferences, you can do it like that:

    To write to the SharedPreferences:

    SharedPreferences sp = context.getSharedPreferences("database", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("Username", username);
                        editor.apply();
    

    To retrieve information:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
                String userstring =  sp.getString("Username",null);
    

    Another problem is the implementation of context is new Intent(v.getContext()) it could be as simple as
    startActivity(this, YourGenderActivity.class));

    This code snippet just uses this for the context then it is similar with next profile context this

    NOTES:

    1. Both writing and reading operations have to have the same Context.
    2. SharedPreferences should write to inside a singleton to make it easy to set and get value
    Login or Signup to reply.
  2. You are using 2 different SharedPreferences objects. You need to get the same one using getSharedPreferences("database", Context.MODE_PRIVATE) in both activities. By using PreferenceManager.getDefaultSharedPreferences(context), you’re using a SharedPreferences object that is associated to your package name (nothing wrong with using that, but I prefer to be explicit).

    Login or Signup to reply.
  3. 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:

    SharedPreferences sp = getSharedPreferences("database", MODE_PRIVATE);
    String userstring =  sp.getString("Username",null);
    

    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 :

    SharedPreferences sp = 
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search