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)

  1. everytime need pass context function in class (for ex class writing sharedpreferences), passing getactivity().getapplicationcontext().
  2. should save variable once in oncreate() of activity (i.e. context ctxt = getactivity().getapplicationcontext()) , use variable ctxt throughout 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

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -