[英]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: 在我的Application onCreate中,我以如下方式实例化Retrofit:
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. 我曾经在RestClient中有一个静态块来对其进行初始化,但是现在我需要为每个请求在Headers中添加应用程序版本,因此我需要在初始化时传递String。 (getAppVersion() needs a Context in order to obtain the app version)
(getAppVersion()需要上下文来获取应用程序版本)
I added a throw clause in RestClient.get() if it hasnt been initialized. 如果尚未初始化,我在RestClient.get()中添加了throw子句。
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? 我的问题是,通常,如果一个Activity闲置数小时,操作系统会杀死它,有时在长时间闲置后恢复活动时,它在onResume中寻找的某些内容为null并崩溃,因此在onResume之前调用Application.onCreate ,活动是否事先被杀死?
This is my RestClient class 这是我的RestClient类
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. 如果您的活动被杀死,则接下来将对该活动调用onCreate。 The Application's onCreate() is only called once to my knowledge.
据我所知,该应用程序的onCreate()仅被调用一次。
So example: I boot up my app and I change the orientation, onDestroy and onCreate are called for only that Activity. 这样的例子:我启动了我的应用程序,并且改变了方向,仅针对该Activity调用onDestroy和onCreate。 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.
除非关闭了整个应用程序,否则没有理由再次调用该应用程序的onCreate()。
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:
但是,我的整个案例都是无效的,因为有一种更轻松的方法可以解决我遇到的问题,而无需将上下文传递给Retrofit,方法如下:
String verName = BuildConfig.VERSION_NAME;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.