android - Can I use an Application subclass as a global variable container? -
android - Can I use an Application subclass as a global variable container? -
i found , read lot of articles talking global variables in android, of them suggests using subclass of application + declare in manifest file glbal variable container.
but articles mentioned class killed when scheme memory gets low, correct?
so, 100% reliable utilize application subclass global variable container? , give me link documents explaining life cycle of application in android (not activity)?
edit:
thanks answers, think need explain bit more of question.
the situation is, want share global string variable, activity modifies it, , activity b reads it.
when b visible , user receives call, if , b killed application maintain untouched (is google calls empty process?), i'm ok it. if a, b, , application class killed , when user come app gets clean start, i'm ok it.
which i'm not ok is, killed including application class, when user come app doesn't start fresh, mean, starts activity b, happen? should start manually or allow application class initiation? none of these ideas looks me...
the reply both "yes" , "no" - application class can used "global variable container" in activities , classes can rely on it.
however, cannot rely on application class (or other class) persist indefinitely. in other words, if user answers phone, application destroyed , re-created when user completes phone call , returns app. so, cannot rely on persist data, can rely on provide global access info when app active.
this android documentation:
http://developer.android.com/training/basics/activity-lifecycle/index.html
this post on - read sec highest voted response, in add-on "accepted" response:
using android application class persist data
it explains pretty how app can killed/destroyed whether expect or not.
edit:
to clarify, if have variable (call "myvar") in application class , user sets in activity a, proceeds activity b, activity b able read change.
if android "destroys" application class, can occur anytime user not in app (and in rare instances if are...), app reconstructed activity stack still valid "myvar" not set, unless persist data.
in other words, android recreate application class , activity b, there no guarantee recreate activity until user destroy activity b. also, not "replay" user actions in activity in order recreate app state, , in case means "myvar" not reliable.
if read references provided, see case (and have tested it).
edit 2:
to persist data, consider sqlite (which pretty complicated , there many references) or sharedpreferences:
how utilize sharedpreferences in android store, fetch , edit values
android global-variables
Comments
Post a Comment