1

I'm working on a SQLiteOpenHelper from which I'll read databases via static methods (since the databases are shared anyway). Is it possible to get the application context to something like:

public static final Context context = XXX;

It should be possible right? Since I'm obviously only calling from the current app and both resources and databases are shared inside the app.

To be clear: I want to access Resources and the SQLiteDatabases (if I happen to be wrong about the context approach).

Is it possible to achieve?

Edit: Is it possible to get the context from inside something like this (without passing it as a parameter)

public class foo{
    foo(){
        XXX.getResources();
    }
}

Edit2: Trying @britzl:s fist idea

public class SpendoBase extends Application{
private static Context context;
public SpendoBase(){
    System.out.println("SPENDOBASE: " + this);
    System.out.println("SPENDOBASE: " + this.getBaseContext());
}
public static Context getContext(){
    return this.context;
}

}

How do i get hold of the context? Either in the constructor or form the getContext();

Ps the getBaseContext() returns null, and getApplicationContext thows a nullPointerException.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
SverkerSbrg
  • 503
  • 1
  • 9
  • 23

1 Answers1

2

I see three possible solutions to your problem:

  1. Create your own subclass of Application and set that as your application class in the manifest file. In your subclass you could have a static getInstance() method that would provide you with the application context (and thus Resources) from anywhere within your application. Example:

    public class BaseApplication extends Application {
    
        private static BaseApplication instance;
    
        public BaseApplication() {
            super();
            instance = this;
        }
    
        public static BaseApplication getInstance() {
            return instance;
        }
    }
    

    And in AndroidManifest.xml:

    <application android:name="com.example.BaseApplication" ...>
        ...activities
    </application>
    
  2. Pass a context to any calls you make in your SQLiteOpenHelper

  3. Inject the Resources instance using dependency injection

Howard
  • 36,183
  • 6
  • 60
  • 81
britzl
  • 9,742
  • 7
  • 38
  • 36
  • 1. Sounds interesting, i'll look in to it 2. Is the mess i'm trying to get straight atm 3. Not quite sure i follow? :) – SverkerSbrg Apr 29 '13 at 11:12
  • I'm trying to your first idea. I'm adding the code in the question, could you please help me to get a hold of the context from either the constructor or a static method call – SverkerSbrg Apr 29 '13 at 11:38
  • Updated with an example for #1 – britzl Apr 29 '13 at 12:05
  • Thank you! This will enable me to build a lot nicer structure :D – SverkerSbrg Apr 29 '13 at 12:07
  • Personally I would have preferred a design that didn't require static access like that, but I'm glad it worked out in your case! – britzl Apr 29 '13 at 17:24
  • :) Why not? i don't REQUIRE it but ended upp with passing around contexts everywhere -.- But this is my fist bigger android project so i'd love you to elaborate. – SverkerSbrg Apr 30 '13 at 10:58
  • If it's a large project there are benefits of using something like dependency injection. It can be used to greatly simplify unit testing but it's also a very powerful way of untangling dependencies. By using dependency injection you don't really have to worry about passing dependency Foo to class Bar or a Context to a SQLiteOpenHelper. The tedious passing of dependencies via constructors, setters or factories are done automatically once a dependency graph has been built. Check: http://stackoverflow.com/questions/130794/what-is-dependency-injection – britzl May 01 '13 at 12:57
  • Also, for Android there's RoboGuice (https://github.com/roboguice/roboguice) for Android specific dependency injection (Contexts, Views, Extras etc). I've used RoboGuice in several project and it's very helpful. I have however ended up using plain old Guice and added Android specific injection myself (I don't like having to inherit from RoboActivity, RoboFragment etc). – britzl May 01 '13 at 12:59