Android sharedpreferences between activities -


hi have problem sharedpreferences because wants keep data entered in edittext in variable , call them in activity use action whole time have problem , want enter program last activity of codes 1, 2 , 3 activity wants take advantage of data.

http://imageshack.us/photo/my-images/402/53l5.jpg/ - made picture of application

1st activity

   sharedpreferences kalorie = preferencemanager.getdefaultsharedpreferences(this);        sharedpreferences.editor editor = kalorie.edit();         editor.putstring("waga", waga.gettext().tostring());         editor.putstring("wiek", wiek.gettext().tostring());         editor.putstring("wzrost",wzrost.gettext().tostring());                 editor.commit(); 

2nd activity

    sharedpreferences tea = preferencemanager.getdefaultsharedpreferences(this);                    sharedpreferences.editor editor1 = tea.edit();                    editor1.putstring("trening", trening.gettext().tostring());                    editor1.putstring("aeroby", aeroby.gettext().tostring());                    editor.commit(); 

3th activity in activity wants take advantage of entered data integer textu edit , execute them action

 sharedpreferences kalorie = preferencemanager.getdefaultsharedpreferences(this);  int wagavalue = integer.valueof(kalorie.getstring("waga", "0")); int wiekvalue = integer.valueof(kalorie.getstring("wiek", "0")); int wzrostvalue = integer.valueof(kalorie.getstring("wzrost", "0")); int kcalwynik = (int) (((( wagavalue * 9.99 ) + ( wzrostvalue * 6.25 )) - ( wiekvalue * 4.92 ))+5);  txtwynik.settext(kcalwynik);  int redvalue = integer.valueof(etreduk.gettext().tostring() );   sharedpreferences tea = preferencemanager.getdefaultsharedpreferences(this);  int treningvalue = integer.valueof(tea.getstring("trening", "0")); int aerobyvalue = integer.valueof(tea.getstring("aeroby", "0"));  int kcalwynik1 = treningvalue+aerobyvalue;   int redukcja = kcalwynik + kcalwynik1; int red1 = redukcja - redvalue;   string returnmsg = red1 + " kcal";  txtwynik1.settext(returnmsg); 

when menu pops choose activity 3 deviation program has stopped

thank corrections , help

1 activity (kalorie - english calories) , 2nd activity (tea) activities draw data user edittext fields in layouts, 1 activity 1 layout , @ end of these 2 activities want retrieved data sent 3 activities mathematical operations , textview display result.

the 2 activities tea had yet radiobuttony depending how 1 chooses variable x took on value not know how save sharedpreferences radiobuttons

here's sharedpref class @rajeev suggested can write single class can handle pref. related code.

sharedpref class


Comments