javaandroidandroid-intentsharedpreferencesextra

How to save the intent Extras in Shared prefrences


Here is my code which request user for their username:

private void request_user_name() {
 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Enter name:");

    final EditText input_field = new EditText(this);
    input_field.setText(sharedpreferences.getString("username",""));
    final SharedPreferences.Editor editor = sharedpreferences.edit();

    builder.setCancelable(false);
    builder.setView(input_field);
    final String savedName = sharedpreferences.getString(username,"");

    input_field.setText(savedName);
    input_field.setSelection(input_field.getText().length());
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            name = input_field.getText().toString();


            if(TextUtils.isEmpty(savedName)) {
                input_field.setError("Your message");
                builder.setCancelable(false);
            }
            editor.putString(username, name);
            editor.apply();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            if(TextUtils.isEmpty(savedName)) {
                input_field.setError("Your message");
                builder.setCancelable(false);
            }
            else
            {
                dialogInterface.cancel();
                request_user_name();
            }

        }
    });
    builder.show();
}

And this is how I pass he username to next Activity:

  intent.putExtra("room_name",((TextView)view).getText().toString() );
            intent.putExtra("user_name",name);
            intent.putExtra("default_name","anyonymous");
            startActivity(intent);

And this is how I receive the Intent Extra in next Activity:

 if (getIntent().getExtras() != null
            && getIntent().getExtras().get("user_name")!=null) {

        user_name =  getIntent().getExtras().get("user_name").toString();

    }else{

        user_name =  getIntent().getExtras().get("default_name").toString();
    }

My problem is when users don't enter his name and it returns null then it returns anyonymous but when users enter his name then it pass user name to next activity. But when I restart the app then again it passes anyonymous to next activity instead of usernae the user already entered

So I need suggestion on how to save the entered username in shared prefrences and use the same in future launch of app unless new name is not entered instead of anyonymous

Thanks in advance.


Solution

  • If you are already using SharedPreferences to store the user_name, then you don't need to pass the user_name via Intent. SharedPreferences are global and could be accessed throughout the application. Just do:

    sharedpreferences.getString("username","anonymous")

    in your target activity.