0

so what im trying to do is take the int "inc" and .putPutExtra("Inc",inc) it to my second class called "Settings" and then save it via "SharedPreferences" and then when the user pushes the button to go back to my main class it auto loads that into a int

heres the code i used to send it to the settings class from MainActivity

int inc = 0;

Intent GoToSettings = new Intent(getApplicationContext(), settings.class);
GoToSettings.putExtra("Inc", inc);
startActivity(GoToSettings)

heres the code that receves it

Intent intent = getIntent();
Inc = intent.getIntExtra("Inc", 10);

heres the code that saves "Inc"

 SharedPreferences Saveinc=getSharedPreferences("IncSave", Context.MODE_PRIVATE);
                SharedPreferences.Editor SaveIncE=Saveinc.edit();
                SaveIncE.putInt("IncSave", Inc);
                SaveIncE.commit();

heres the code that receves the int after its saved

    int inc = 1;

    Intent GoToMain = getIntent();
    inc = GoToMain.getIntExtra("IncSave", 1);


SharedPreferences GetSavedinc=getSharedPreferences("IncSave", Context.MODE_PRIVATE);
int  IncSave=GetSavedUpCount.getInt("IncSave", 1);
inc=IncSave;

i no its not the prittyest layout but im new and havent really learned the syntex perfictly. im also new to the website to so if i dissobay a rule im sorry.

jason flanagan
  • 117
  • 1
  • 1
  • 9
  • 1
    But in your second activity when you retrieve your variable and save in shared preference then no need to get it through `Intent GoToMain = getIntent(); inc = GoToMain.getIntExtra("IncSave", 1);` in another activity. – Piyush Aug 20 '14 at 05:06
  • Why don't you just read it from `SharedPreferences` again? – Sadegh Aug 20 '14 at 05:29
  • Refer this link to know more about shared preferences http://stackoverflow.com/questions/24772291/how-to-save-json-from-url-and-update-the-saved-file-from-url-after-fixed-interva/24772552#24772552 – Paritosh Aug 20 '14 at 05:37

3 Answers3

0

if your int 'inc' is going to end up in your SharedPreferences in the end no matter what, you could just skip passing it to the other class and just write it into your prefs in your main activity.

You can just read it from shared prefs during the onCreate/onResume when you return to your main activity, and you can read it from your shared prefs in the constructor of your settings class.

Will Thomson
  • 875
  • 6
  • 19
0

In your last activity where you just want value from SharedPreference in that just use this.

//int inc = 1;
//Intent GoToMain = getIntent();                     //Comment all this..
//inc = GoToMain.getIntExtra("IncSave", 1);


SharedPreferences GetSavedinc=getSharedPreferences("IncSave", Context.MODE_PRIVATE);
int  IncSave=GetSavedUpCount.getInt("IncSave", 1);
Log.v("My Value",IncSave+"");
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

If you want to send some data between 2 activities, you should never use shared pref. Below is prefered way to do this : Send data from Activity1 to Activity2 :

Intent intent = new Intent();
intent.putExtra("numberFromActivity1", 122);
startActivityForResult(intent, REQUEST_CODE);

In Activity2, read this using below code :

    Intent intent = getIntent();
    int inc = intent.getIntExtra("numberFromActivity1", 0); // 122

Send data from Activity2 to Activity1 : You can set result in onDestroy() or in onBackpressed() methods.

Intent intent = new Intent();
intent.putExtra("numberFromActivity2", 221);
setResult(RESULT_OK, intent);
finish(); // Finish second activity

Now in Activity1, you can get this data in onActivityResult() method :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if(requestCode == REQUEST_CODE) {
         int numberFromActivity2 = data.getIntExtra("numberFromActivity2" , 0); // 221
     }
}
Harry
  • 422
  • 3
  • 11