java - What is the best way to get Context throughout an Activity? -
i save sharedpreferences within single activity several times. each time reads or writes, pass context. following 2 options thinking - 1 better (or there better way)
- everytime need pass context function in class (for ex class writing sharedpreferences), passing
getactivity().getapplicationcontext(). - should save variable once in
oncreate()of activity (i.e.context ctxt = getactivity().getapplicationcontext()) , use variablectxtthroughout activity whenever need pass context.
which way technically better? key - should accessing getapplicationcontext() each time. guessing slow (& unnecessary) , causes crashes (i've been seeing logs users unable reproduce stack traces point lines containing getapplicationcontext())
the activity can used context. pass this. can pass activityname.this inside inner class.
getapplicationcontext() returns "the context of single, global application object of current process."
Comments
Post a Comment