Dealing With Application Context
I'm new to android and java. I'm rearranging some of the classes in my app into separate class files. I had a onLocationListener class in my main activity class file. I moved the
Solution 1:
getApplicationContext() is a method of class Context, so you can only call it from a class or object that in some way extends Context
. You factored your code out of Activity, which is such a class. Your solution, then, is for the Context
class that contains your new class or object to pass its context in so that your new class can use it.
Your code inside your main Activity would look something like this:
MyOwnClassownObject=newMyOwnClass();
// you have to implement setApplicationContext
ownObject.setApplicationContext( this.getApplicationContext() );
It's probably a good idea to get the application context right away, since it'll be stable for the lifetime of your app, unlike the Activity context which could go away on something as simple as an orientation change.
Post a Comment for "Dealing With Application Context"