JRad the Bad
JRad the Bad

Reputation: 511

Getting resources from non-activity classes

I have a special class that holds static info and makes calculations based on special circumstances. This is a special custom class that does not extend any part of the activity or android environment whatsoever.

Because this class never really gets instantiated, it's mostly referred to on a static level. It's still important for me because I hold a number of static enums that are vital to the application's flow.

Here's the issue: Because the class doesn't extend the android activity life cycle, I'm having trouble referencing any string values from the Application's resource files. (I have strings stored in custom XML files as special resources)

Here's how everything looks:

CUSTOM CLASS FOR ENUM:

public class CreepIDs {

    public static Context context = App.getContext();

    public static enum CreepId {

        ROBODEE(0, resourceString(R.creeps.robodee));

        public final int id;
        public final String name;

        CreepId(int id, String name){
            this.id = id;
            this.name = name;
        };
    };

    public static String resourceString(int id){
        return context.getResources().getString(id);
    }
}

APP CLASS EXTENDING APPLICATION:

public class App  extends Application{

    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }

    public static Context getContext(){
        return mContext;
    }
}

I've stepped through the app so far using the debug feature and App.getContext() is always returning a Context with a value of null. I can't figure out why. I need to be able to reference the R.creeps.robodee from a class that doesn't extend any part of the android lifecycle.

I would pass the context through the constructor, but since CreepId is a static enum CreepIDs doesn't actually get instantiated. I only reference it from the main activity.

What do you think is the best solution here?

Upvotes: 0

Views: 2039

Answers (4)

Fubbe99
Fubbe99

Reputation: 35

This link fixed everything for me: How can I get a resource content from a static context?

I just had to realize that I should create a completely new class with nothing but the code in Cristians answer. Remember to modify the Manifest!

Good luck:)

Upvotes: 0

JRad the Bad
JRad the Bad

Reputation: 511

So it's not pretty but I found a solution. In onCreate for the Main activity (The only place this info is even getting used right now), I've put the following code:

CreepIDs.context = getApplicationContext();

Since context is static public anyways, I'm directly assigning a value to it when the app starts. I only need the context so I can call resources and it won't be used for anything else so I don't think this will be a problem for me at all.

In the end, using the special App class just didn't work for me.

Thanks for everyone's assistance.

Upvotes: 1

krodmannix
krodmannix

Reputation: 845

Based off of Andrey's answer:

public class App extends Application {

private static App INSTANCE;

@Override
public void onCreate() {
    super.onCreate();
    INSTANCE = this;
}

public static App getInstance() {
    return INSTANCE;
}

}

then, in CreepsID:

public class CreepIDs {

public static Context context = App.getInstance();

public static enum CreepId {

    ROBODEE(0, resourceString(R.creeps.robodee));

    public final int id;
    public final String name;

    CreepId(int id, String name){
        this.id = id;
        this.name = name;
    };
};

public static String resourceString(int id){
    return context.getResources().getString(id);
}

}

You're calling a static method to get the context of App, thus giving your static class access to the resources of the Activity.

Upvotes: 0

Andrii Mishchenko
Andrii Mishchenko

Reputation: 2694

Try this:

public class App extends Application {

    private static App INSTANCE;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
    }

    public static App getInstance() {
        return INSTANCE;
    }

}

It works good for me.

Upvotes: 0

Related Questions