简体   繁体   English

如果应用程序闲置了数小时并被OS破坏,是否在Activity.onResume之前调用Application.onCreate?

[英]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.

相关问题 Android Activity::onCreate 在 Application.onCreate 之前调用 - Android Activity::onCreate called before Application.onCreate 在Application.onCreate代码之前调用NotificationListenerService - NotificationListenerService called before Application.onCreate code 如何指示操作系统在应用程序闲置数小时后不要重新创建活动,而要重新启动? - How to instruct the OS not to recreate activity after application has been idle for hours, but to start fresh instead? 在BroadcastReceiver.onReceive(..)之前是否会调用Application.onCreate(Bundle)? - Will Application.onCreate(Bundle) be called before BroadcastReceiver.onReceive(..)? 应用程序可以在Application.onCreate()之前获取Fragment.onAttached()吗? - Can the app get Fragment.onAttached() before Application.onCreate()? 未调用活动onCreate和onResume - Activity onCreate and onResume not called Application.onCreate被多次调用 - Application.onCreate called more than once 为什么 ContentProvider.onCreate() 在 Application.onCreate() 之前被调用? - Why does ContentProvider.onCreate() get called before Application.onCreate()? Application.onCreate() 未在调试版本中调用 - Application.onCreate() not called in debug build 确定调用Application.onCreate()的进程 - Determine by which process Application.onCreate() is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM