简体   繁体   中英

Is Application.onCreate called before Activity.onResume if app has been idle for hours and destroyed by OS?

In my Application onCreate I instantiate Retrofit like so:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        RestClient.setupRestClient(getAppVersion(this));
    }


}

I used to have a static block in RestClient that initializes it, but now I need to add the app version in the Headers for every request, so I need to pass the String at initialization time. (getAppVersion() needs a Context in order to obtain the app version)

I added a throw clause in RestClient.get() if it hasnt been initialized.

My question is, usually if an Activity stays idle for hours, OS kills it, and sometimes when resuming activity after a long idleness, some stuff that it is looking for in onResume are null and it crashes, so is Application.onCreate called before onResume, if the activity has been killed beforehand?

This is my RestClient class

public class RestClient {

    private static API REST_CLIENT;

    private RestClient() {
    }

    public static API get() {
        if(REST_CLIENT==null){
            throw new IllegalStateException("Rest Client not initialized");
        }
        return REST_CLIENT;
    }

    public static void setupRestClient(final String appVersion) {

        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Story.class, new StorySerializer())
                .create();


        RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader(ServerKeys.HEADER_OS_VERSION, ServerKeys.HEADER_OS_VERSION_VALUE_ANDROID);
                request.addHeader(ServerKeys.HEADER_APP_VERSION, appVersion);
            }
        };

        RestAdapter.Builder builder = new RestAdapter.Builder();
        builder.setEndpoint(APIKeys.API_ROOT);
        builder.setRequestInterceptor(requestInterceptor);
        builder.setExecutors(Executors.newFixedThreadPool(Preferences.MAX_NUMBER_OF_PARALLEL_NETWORK_OPERATIONS), new ScheduledThreadPoolExecutor(Preferences.MAX_NUMBER_OF_PARALLEL_NETWORK_OPERATIONS));
        builder.setConverter(new GsonConverter(gson));
        RestAdapter restAdapter = builder.build();
        REST_CLIENT = restAdapter.create(API.class);
    }

}

And this is how I use it:

RestClient.get().resetUserPassword(ge....

If your activity is killed then onCreate is called next for that Activity. The Application's onCreate() is only called once to my knowledge.

So example: I boot up my app and I change the orientation, onDestroy and onCreate are called for only that Activity. The application was never restarted.

So to answer you question now that I've read it more carefully I would think not. Unless the entire application was closed there should be no reason for the application's onCreate() to be called again.

The direct answer to the question is YES, like Selvin said in the comments. However, my entire case was invalid because there was a much easier way to solve the problem I was having without the need to pass the context to Retrofit, here is how:

String verName = BuildConfig.VERSION_NAME;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM