android - Edit sharedPreferences with two different activities to affect change in third activity -
i know sharedpreferences, confused this. know put things sharedpreferences, this:
let's activity a:
sharedpreferences preferences=getsharedpreferences("numbers",mode_private); sharedpreferences.editor edit=preferences.edit(); string a=ed.gettext().tostring(); string b=ed1.gettext().tostring(); editor.putstring("num1",a); editor.putstring("num2",b); editor.apply();
and out in activity b,
sharedpreferences preferences=getsharedpreferences("numbers",mode_private); string numberone=preferences.getstring("num1",""); string numbertwo=preferences.getstring("num2","");
and set textview in activity b :
textview both; both.settext(numberone + " " + numbertwo);
but if want edit in sharedpreferences in activity c?? looking online on how , article here
http://codetheory.in/android-application-data-storage-sharedpreferences/
it said had call again different string value so
activity c:
sharedpreferences preferences=getsharedpreferences("numbers",mode_private); sharedpreferences.editor edit=preferences.edit(); string c=ed3.gettext().tostring(); string d=ed4.gettext().tostring(); editor.putstring("num1",c); editor.putstring("num2",d); editor.apply();
and when done, should update add in activity b 29 , not 12 more. problem is, not edit sharedpreferences. there way this? why won't update addition in activity b?
you loading values in oncreate()
that method gets called 1 activity lifetime. when rotate phone activity gets destroyed , rebuilt, calling oncreate()
again.
a possible solution load values in onresume()
way have them come other activity.
Comments
Post a Comment