android - How do I modify one activity's variables from another activity? -
let's first.class has variable string currentvalue = "red" button leads second.class (an activity). first.class(activity) displays in textview variable currentvalue happens be. (currently, red).
if push button, takes second.class, has edittext box modify variable in first.class. has button confirm change. finally, has textview @ bottom showing preview of first.class' value variable is.
when user types in "blue" in second.class' edittext box , hits button, how change variable first.class without using intents , going activity? want stay within second.activity , changes there.
after hitting confirm button, preview textview should update match newly modified variable. should still seeing second.class, remind you. if user hits "back" or "up" @ point, should return first.class , see textview in first.class has been changed.
how modify first.class' variables if second.class entirely separate first.class , cannot access it? (first.class hierarchical parent of second.class.
how modify first.class' variables if second.class entirely separate first.class , cannot access it?
you can't or (more importantly) should not try this.
the android activity
"special case" class , should considered being self-contained. in other words changes data in second activity
need reflected in first activity
must either persisted using form of global storage (sharedpreferences
example) or should passed using extras of intent
or bundle
.
with sharedpreferences
have first activity
save currentvalue
before starting second activity
, reverse in second activity
before returning first. first activity
needs check sharedpreferences
in onresume()
, update textview
if necessary.
as codemagic mentioned, however, using startactivityforresult(...)
allow passing currentvalue
first second activity
and, before second exits, updating bundle
changes allow passed first activity
through onactivityresult(...)
.
Comments
Post a Comment