30

I'd like to get my string-array without extending Activity in my custom class. Is there a way to do this?

String[] foo_array = getResources().getStringArray(R.array.foo_array); will not work without extending Activity, so I need a work-around.

0

4 Answers 4

47

Pass the context to the constructor of custom class and use the same

new CustomClass(ActivityName.this);

Then

Context mContext;
public CustomClass(Context context)
{
    mContext = context;
}

use the context

String[] foo_array = mContext.getResources().getStringArray(R.array.foo_array);

Also keep in mind

Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)

http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html

Also check this

android getResources() from non-Activity class

Edit:

Change this

public class CustomClass(Context context) 
{
}

To

public class CustomClass
{
   Context mContext;
   public CustomClass(Context context) // constructor 
   {
    mContext = context;
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

When I change my custom class to public class CustomClass(Context context) {} it gives me errors saying Syntax error on token "(", { expected.
@JosephWebber you need to have a constructor not alter the synxtax of class declaration. check the edit
@JosephWebber check the edit it should work and also check this docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
@ Raghunandan Thanks. I misinterpreted what you said, but I remember how to do it now.
9

try this,

Context context=getApplicationContext();
String[] foo_array = context.getResources().getStringArray(R.array.foo_array);

And, do not use Activity Context as that is tied to the Activity life cycle.

Update,

getApplicationContext() is from Context class. That means any thing extended Context have this method. This also means you will be able to use this from service or from other resources.

But, if you custom class do not extend Activity/context, you have to pass Context as parameter to use getApplicationContext()

if you declare your activity like this

myMethod(Activity activity) //this is bad

Bud if it is like following,

myMethod(Context context) //this is ok

but from above declaration do not pass Activity or Service Context as they have own life cycle. instead you will use getApplicationContext()

2 Comments

Context context=getApplicationContext(); then gives the error The method getApplicationContext() is undefined for the type CustomClass.
why is this myMethod(Activity activity) bad? developer.android.com/reference/android/content/Context.html. Activity is a known indirect subclass of Context. stackoverflow.com/questions/7298731/…
0

You need pass the Activity context to the Custom class.

private Context context;
public CustomClass(Context context)
{
 this.context=context;
}

Comments

0

if you use numberpicker and pass String from sring xml then use this

np_Basic_Hight.setMinValue(0);

    np_Basic_Hight.setMaxValue(71);
    np_Basic_Hight.setDisplayedValues(getContext().getResources().getStringArray(R.array.hieght));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.