Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Race condition between Application onCreate and resources loaded?

Tags:

android

I have the following application class for my app. When the application starts, I want to get some settings from preferences and start a background service.

public class MyApplication extends Application {

    public void onCreate() {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            String key = getResources().getString(R.string.prefkey_updateinterval);
            ...
    }

This normally works fine, but occasionally when starting my program from eclipse "Run" I get this error:

10-10 08:25:47.016: E/AndroidRuntime(26402): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f0a0004
10-10 08:25:47.016: E/AndroidRuntime(26402):    at android.content.res.Resources.getText(Resources.java:216)
10-10 08:25:47.016: E/AndroidRuntime(26402):    at android.content.res.Resources.getString(Resources.java:269)
10-10 08:25:47.016: E/AndroidRuntime(26402):    at com.karwosts.MyApp.PortfolioStore.onCreate(PortfolioStore.java:40)
10-10 08:25:47.016: E/AndroidRuntime(26402):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
10-10 08:25:47.016: E/AndroidRuntime(26402):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3395)

This id is from my R.java:

public static final int prefkey_updateinterval=0x7f0a0004;

Since this works fine most of the time, I have to assume that there is some kind of race condition between onCreate and the resources being loaded?

If that's the case, is it recommended not to read resources in Application onCreate?

If so, is there a better place to initialize a service when application launches?

like image 803
Tim Avatar asked Oct 10 '12 15:10

Tim


2 Answers

Since this works fine most of the time, I have to assume that there is some kind of race condition between onCreate and the resources being loaded?

If the same APK file -- no recompile, no reinstall, etc. -- does not consistently generate the error, then it might be some sort of race condition, though that would surprise me.

If the same APK file consistently fails, then this is a more garden-variety resources-out-of-sync-with-the-rest-of-the-code problem, and cleaning the project will clear it up.

is there a better place to initialize a service when application launches?

IMHO, the number of apps that need to "initialize a service when application launches" is extremely low, to the point where I cannot think of a scenario off the cuff where this would be a good plan. That's not to say that you do not have such a scenario, but it's a serious code smell in my book without an explanation.

like image 87
CommonsWare Avatar answered Oct 03 '22 08:10

CommonsWare


I've noticed that in cases where a lot of resources are used (and generated in R.java), cleaning the application before running fixes these issues. So I would assume this is not a race condition - but an issue with Eclipse or the Android SDK with not refreshing resources. As for the placement of the code - it's is as good as any other option, in my opinion.

like image 43
Phil Avatar answered Oct 03 '22 07:10

Phil