TpoM6oH
TpoM6oH

Reputation: 8585

Final field initialization in an Activity constructor

Is there any problem in this code?

public class MyActivity extends Activity {
    private final String[] names;

    public MyActivity() {
        names = new String[]{
            getString(R.string.strion_one),
            getString(R.string.string_two),
            getString(R.string.string_three),
        };
    }    

    ...

}

Upvotes: 1

Views: 550

Answers (1)

user2558337
user2558337

Reputation:

If i right understand your problem, you try to create a class, which contains links on your resources. So i recommend you to create a class which will be contains links on your resources.

 public class MyResources{

    private final String[] myStrings;
    public MyResources(Context context)
    {
        myStrings = new String[]{};
        myStrings[0] = context.getResources().getString(R.string.string_one);
        myStrings[100] = context.getResources().getString(R.string.string_one_hun);
    }

    public String getStringAtNumber(int value)
    {
        return myStrings[0];
    }
}

And yes! One more thing. You can get resources only after onCreate execute.

Upvotes: 2

Related Questions