Home > Mobile >  can't called the saved SharedPreferences value
can't called the saved SharedPreferences value

Time:09-26

i have a problem that when i call the saved share sharedPreferences integer value in second activity its return 0 value !!!

this the code in first activity(saving )

public void startClick(View view) {
   Toast.makeText(this,String.valueOf(l),Toast.LENGTH_SHORT).show();
   
    
    if (language == 1 && isCountrySelected==true )   {
        //Saving Value
        SharedPreferences sharedPreferences = getSharedPreferences("indemnity_calculation_kuwait",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("languageValue", l).apply();
        editor.commit();

        startActivity(intentKuwaitAr);
        finish();



       // openArabicActivity();
       // finish();
    }

in second activity "splash screen" im trying to call the saved value

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    intentKuwaitEn = new Intent(this ,MainActivity.class) ;
    intentKuwaitAr = new Intent(this,ArabicActivity.class) ;
    intentMenu = new Intent(this,MenuActivity.class) ;

  //  Toast.makeText(this,"l=" String.valueOf(savedValue),Toast.LENGTH_SHORT).show();

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
   int savedValue = sharedPreferences.getInt("languageValue", 0);
    Toast.makeText(this,"l=" String.valueOf(savedValue),Toast.LENGTH_SHORT).show();


    Thread myThread= new Thread(){
        @Override
        public void run() {
            try {
                sleep(3000);
                if (savedValue==0){startActivity(intentMenu); finish();}
                if (savedValue== 1){startActivity(intentKuwaitAr); finish();}
                if (savedValue ==2){startActivity(intentKuwaitEn); finish();}
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    myThread.start();
}

CodePudding user response:

In first activity you are putting language value in indemnity_calculation_kuwait.

getSharedPreferences("indemnity_calculation_kuwait",MODE_PRIVATE);

and other activity you are fetching from

getPreferences(MODE_PRIVATE);

Please use below shared pref in second activity too. Thank you.

getSharedPreferences("indemnity_calculation_kuwait",MODE_PRIVATE); 

CodePudding user response:

i found the problem

need to add App name

 SharedPreferences sharedPreferences = getSharedPreferences("indemnity_calculation_kuwait",MODE_PRIVATE);
  • Related