4

I am using the following piece of code in my app:

string_array = getResources().getStringArray(R.array.array_of_strings);
my_text_view.setText(string_array[i]);

The problem is that I start the activity which contains this code many times. So, I feel that this code is inefficient as every time i start the activity i have to load the whole array to just use one element specially as the string array is very large.

Is there a way to load the wanted element directly?

Thanks a lot in advance :)

update: I tried using Application object as Talihawk stated below, but i get an error at the following line:

MyApplication my_obj = (MyApplication) getApplication();
string_array = my_obj.getStringArray(); 

The Error is:

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

1
  • Every time i start this activity (which i start it a lot), I load a large string array and then use one element. Is there a way to load the wanted element directly? Commented Mar 2, 2012 at 18:46

3 Answers 3

9

You can use the Application object to save the string array for as long as your application is active.

** EDIT**

First you have to define your custom Application class in your AndroidManifest.xml :

<application
    android:name="MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
>

** EDIT **

public class MyApplication extends Application {

    private String[] string_array;

    @Override
    public void onCreate() {
        super.onCreate();
        string_array = getResources().getStringArray(R.array.array_of_strings);
    }

    public String[] getStringArray() {
        return string_array;
    }
}

And in your activity just do :

string_array = ((MyApplication) getApplication()).getStringArray();
my_text_view.setText(string_array[i]);

This way your string array will only be loaded once for the duration of your application (if your application is killed you'll have to reload it anyway)

You can find more information in the following link

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot :) but I get an error when reaching the following line: MyApplication my_obj = (MyApplication) getApplication(); line string_array = my_obj.getStringArray(); android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) Thanks a lot ... I really appreciate your help :)
@Elshaer Can you post a stacktrace of your eror?
@Elshaer I think I know why you're getting an error and edited my answer with another missing part of the solution. Hope this works :)
1

Use Shared Preferences. Any questions: http://developer.android.com/guide/topics/data/data-storage.html#pref

Comments

1

You could create a helper object with a static method to load/return the array of strings, therefore it would only be loaded once. If you wanted to get real fancy you could declare it as a soft or weak reference so it won't hog memory, but that is your call...depends on the size of the array and the app.

public class StringHelper {
    private static String[] stgArray;
    public static String[] getMyStringArray( Context ctx ) {
        if ( stgArray == null ) {
            stgArray = ctx.getResources().getStringArray(R.array.array_of_strings);
        }
        return stgArray;
    }

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.