Search code examples
androidsharedpreferencesandroid-sharedpreferences

SharedPreferences does not permanently save data


I make a change of data in the SharedPreferences function and when I go to call in the same activity the data is up to date, but when the exchange activity is not up to date.

I use this feature to update the data:

public static String setVal(Context context, String val){
        String LOGIN = "Login";
        SharedPreferences pref = context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor2 = pref.edit();
        editor2.remove("creditiApp");
        editor2.putString("creditiApp",val);
        editor2.apply();
        editor2.commit();

        String LOGIN2 = "Login";
        SharedPreferences pref2 = context.getSharedPreferences(LOGIN2, Context.MODE_PRIVATE);
        return pref2.getString("creditiApp","0");
    }

I call this function in an asynctask, more precisely in the "onPostExecute":

protected void onPostExecute(String result) {
            String[] results = result.split("///---");
            if( results[0].equals("true") ){
                String LOGIN = "Login";
                SharedPreferences pref = getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
                String crediti22 = pref.getString("creditiAPP","no");
                codiceEdit.setText(results[1]);
                Bitmap bit = img.getBitmapFromURL(results[2]);
                Bitmap bit2 = img.getRoundedCornerBitmap(bit,500);
                imgTonda.setImageBitmap(bit2);
                titolo.setText(results[3]);
                autore.setText(results[4]);
                int creditiNecessari = Integer.valueOf(results[5]);
                int creditiDisponibiliDB = Integer.valueOf(results[6]);
                int creditiDisponibiliApp = Integer.valueOf(crediti22);
                if( creditiDisponibiliDB == creditiDisponibiliApp ){
                    crediti.setText(getString(R.string.pubbCrediti)+" "+creditiNecessari);
                    crediti2.setText(getString(R.string.pubbCrediti2)+" "+creditiDisponibiliApp);
                }else{
                    String str = setVal(getApplicationContext(),String.valueOf(creditiDisponibiliDB));
                    Log.d("crediti",str );


                    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                    View header = navigationView.getHeaderView(0);
                    TextView crediti_view = (TextView) header.findViewById(R.id.creditiMenu);
                    crediti_view.setText(getString(R.string.creditiDisp)+" "+creditiDisponibiliDB+" "+getString(R.string.crediti));
                    crediti.setText(getString(R.string.pubbCrediti)+" "+creditiNecessari);
                    crediti2.setText(getString(R.string.pubbCrediti2)+" "+creditiDisponibiliDB);
                }
                if( creditiDisponibiliDB >= creditiNecessari ){
                    guadagna.setVisibility(View.GONE);
                    download.setVisibility(View.VISIBLE);
                    download.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(pubblicita.this,home.class);
                            intent.putExtra("codice",codice);
                            startActivity(intent);
                            finish();
                        }
                    });
                }else{
                    guadagna.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            intro.setVisibility(View.GONE);
                            showProgress(true);
                            pubb.setVisibility(View.VISIBLE);
                            showProgress(false);
                            closePubb.setVisibility(View.GONE);
                            barTimer.setVisibility(View.VISIBLE);
                            timeText.setVisibility(View.VISIBLE);
                            barTimer.startAnimation(an);
                            startTimer(1);
                        }
                    });
                }
                intro.setVisibility(View.VISIBLE);
            }else{
                crediti.setVisibility(View.GONE);
                crediti2.setVisibility(View.GONE);
                autore.setVisibility(View.GONE);
                guadagna.setVisibility(View.GONE);
                titolo.setText(getString(R.string.errorePubblicita));
                intro.setVisibility(View.VISIBLE);
            }
            showProgress(false);
            mAuthTask = null;
        }

Also in the log the data is updated, but when calling another activity the data is not updated:

@Override
    protected void onStart(){
        super.onStart();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        View header = navigationView.getHeaderView(0);
        TextView nome_view = (TextView) header.findViewById(R.id.nome_view);
        TextView email_view = (TextView) header.findViewById(R.id.email_view);
        TextView crediti_view = (TextView) header.findViewById(R.id.creditiMenu);
        String LOGIN = "Login";
        SharedPreferences pref = getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
        String nome = pref.getString("nomeAPP","ciao");
        String cognome = pref.getString("cognomeAPP","ciao");
        String email = pref.getString("emailAPP","ciao");
        String crediti = pref.getString("creditiAPP","ciao");
        String nome_completo = nome+" "+cognome;
        nome_view.setText(nome_completo);
        email_view.setText(email);
        crediti_view.setText(getString(R.string.creditiDisp)+" "+crediti+" "+getString(R.string.crediti));
    }

Do you have any advice to give me?


Solution

  • Check your keys sharedpreference keys are case sensitive here i seeing two diff keys

    creditiAPP creditiApp

    do above change it will work