Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Preferences Android Studio

For the code below I am trying to retrieve the shared preference, I think it saved correctly but when I go back to the login screen all the data is gone. I need it to remain when I go back to this screen. So i enter name, age and id into three separate lines on the profile page. Then i press the save button Then go to the page before by pressing back on the action bar. And when i go back to the profile page my info should still be there but its not Any help?

 package com.example.myprofile;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Savepoint;

public class Profile extends AppCompatActivity {

             protected EditText NameEditText;
             protected EditText AgeEditText;
             protected EditText IDEditText;
             protected Button saveButton;
             protected Button settings_id;
             String name;
             String age;
             String id;

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

        EditText mEdit = (EditText) findViewById(R.id.NameEditText);
        mEdit.setEnabled(false);
        EditText mEdit1 = (EditText) findViewById(R.id.AgeEditText);
        mEdit1.setEnabled(false);
        EditText mEdit2 = (EditText) findViewById(R.id.IDEditText);
        mEdit2.setEnabled(false);

        NameEditText = (EditText) findViewById(R.id.NameEditText);
        AgeEditText = (EditText) findViewById(R.id.AgeEditText);
        IDEditText = (EditText) findViewById(R.id.IDEditText);
        settings_id = (Button) findViewById(R.id.settings_id);
        saveButton = (Button) findViewById(R.id.SaveButton);



         SharedPreferences prefs = getSharedPreferences(getString(R.string.ProfileName), Context.MODE_PRIVATE);
         name = prefs.getString("userName", "");
         age = prefs.getString("userAge", "");
         id = prefs.getString("userID", "");


        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                String name = NameEditText.getText().toString();
                String age = AgeEditText.getText().toString();
                String id = IDEditText.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.ProfileName), Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(getString(R.string.ProfileName), name);
                editor.putString(getString(R.string.ProfileAge), age);
                editor.putString(getString(R.string.ProfileID), id);
                editor.apply();


                  if (Integer.parseInt(age) < 18)
                {
                    Toast toast1 = Toast.makeText(getApplicationContext(), "Invalid Age", Toast.LENGTH_LONG);
                    toast1.show();
                }
                  else if (!name.isEmpty() && !age.isEmpty() && !id.isEmpty())
                  {
                      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                      Toast toast = Toast.makeText(getApplicationContext(), "Name Saved!", Toast.LENGTH_LONG);
                      toast.show();
                  }
                  else
                {
                    Toast toast2 = Toast.makeText(getApplicationContext(), "Incomplete Info", Toast.LENGTH_LONG);
                    toast2.show();
                }


            }
        });

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings_id:
            {
                EditText mEdit = (EditText) findViewById(R.id.NameEditText);
                mEdit.setEnabled(true);
                EditText mEdit1 = (EditText) findViewById(R.id.AgeEditText);
                mEdit1.setEnabled(true);
                EditText mEdit2 = (EditText) findViewById(R.id.IDEditText);
                mEdit2.setEnabled(true);
                saveButton.setEnabled(Boolean.parseBoolean("True"));
            }
            default:
                return super.onOptionsItemSelected(item);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;

    }
}

1 Answers

For save

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("keyname",true);
editor.putString("keyname","string value");
editor.putInt("keyname","int value");
editor.putFloat("keyname","float value");
editor.putLong("keyname","long value");
editor.commit();

For get

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
pref.getString("keyname",null);
pref.getInt("keyname",0);
pref.getFloat("keyname",0);
pref.getBoolean("keyname",true);
pref.getLong("keyname",0);

For single delete

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.remove("keyname");
editor.commit();

For all delete

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
like image 125
Aftab Alam Avatar answered Sep 19 '25 16:09

Aftab Alam