android - Using SharedPreferences and/or class variables in an Activity -
just random question. i'm learning bit of android right now, , in examples, seems lot of common items (such buttons, editboxes etc) requested within each function using (cast) findviewbyid()
.
is considered or bad practice store result of in activity's member values? simple example:
public class myactivity extends activity { private edittext mytext; public void oncreate(blah blah) { // blah this.mytext = (edittext) findviewbyid(r.id.mytext); } }
and use mytext field there on. think it'd performance (depending on findviewbyid's inner workings, i'm quite sure it's fast), haven't seen encouraged yet. also, wouldn't first time encountered situation 'caching' leads problems (had case database connections weren't released because remembered connectionmanager
or in fashion).
secondly, related, if want remember across methods in activity (and later on too, when activity restarted later), wiser keep both class field , value in sharedpreferences, or calling sharedpreferences each time setting / getting value it's needing better solution? (better being 'cleaner, without impacting performance significantly)
that normal practice , should doing. if you're worried memory leaks, or holding references or whatever, don't concerned when dealing views.
however, should careful holding references other contexts because cause memory leak. doesn't mean shouldn't it, careful when you're doing it.
is wiser keep both class field , value in sharedpreferences, or calling sharedpreferences each time setting / getting value it's needing better solution?
you should both. should keep member variable when need read data, sure when write member variable, change shared preference.
also, wouldn't first time encountered situation 'caching' leads problems (had case database connections weren't released because remembered connectionmanager or in fashion).
this saying first. depends on you're storing. views fine store, contexts can dangerous, , database connections , registered listeners can cause weird bugs. depends on specific case.
Comments
Post a Comment