androidandroid-sharedpreferences

Can't retrieve data from SharedPreferences?


I have this Activity ::

public class MainActivity extends AppCompatActivity {

    boolean networkState;

    public View layout;

    TextView Data1Text, Data2Text;

    String Data1, Data2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LayoutInflater inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.toast,
                (ViewGroup) findViewById(R.id.toast_layout_root));

        Data1Text = (TextView) findViewById(R.id.data1);
        Data2Text = (TextView) findViewById(R.id.data2);

        ConnectivityManager cm =
                (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();

            if(isConnected) {
            new doIt().execute();
            networkState = true;
            } else {
            Toast.makeText(this, "NO CONNECTION", Toast.LENGTH_SHORT).show();
            networkState = false;
             }
    }


    public class doIt extends AsyncTask<Void, Void, Void> {


        ProgressDialog mProgressDialog;

        @Override
        protected Void doInBackground(Void... params) {

            try {
                //Here I retreive data from my website and it works like magic
                // I get Data1 and Data2 values from it.
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            Data1Text.setText(Data1);
            Data2Text.setText(Data2);

            mProgressDialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            //mProgressDialog.setTitle("");
            mProgressDialog.setMessage("Update in Progress..");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }
    }


    @Override
    public void onPause() {
        super.onPause();

        SharedPreferences settings;
        SharedPreferences.Editor editor;
        settings = this.getSharedPreferences("MYTAGI", Context.MODE_PRIVATE); //1
        editor = settings.edit();

        editor.putString("data1", Data1);
        editor.putString("data2", Data2);

        editor.commit();
    }
}

How to retrieve data again from my shared preferences? I did it in the last line of onCreate but it does not work...

Saving data seems to be working but retrieving data is not ok .. please help


Solution

  • Here's how I do it. Try saving your data with:

    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
    
    editor.putString("data1", Data1);
    editor.putString("data2", Data2);
    
    editor.commit();
    

    And getting it with:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String Data1 = preferences.getString("data1", "Default value for data1");
    String Data2 = preferences.getString("data2", "Default value for data2");