在Application类中创建实例

时间:2016-08-31 05:58:40

标签: android singleton android-application-class

我正在开发一个Android应用程序,我正在使用网络库Retroift与GSON,Otto和Realm。所以我在应用程序类中创建实例(扩展Application)。它使我的应用程序启动时间非常慢。是否有任何替代解决方案来解决这个问题。

 public class MyApp extends Application{

     private MyApp appInstance;
     private MainThreadBus bus;
     private RealmConfiguration myAppRealmConfig;
     private GSON gson;
     private HttpLoggingInterceptor logging;


     @Override
        public void onCreate() {
            super.onCreate();

            //Application instance
            appInstance = this;
            gson = new GsonBuilder()
                    .setDateFormat(ApiConstants.API_DATE_FORMAT).setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                    .serializeNulls()
                    .create();
            mAppRealmConfig =   RealmConfigurationFactory.createMyAppRealmConfiguration(this, secretKey);
            appConfig = new AppConfig();  logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        bus = new MainThreadBus();

    }

   public Gson getGson() {
        return gson;
    }

    public AppConfig getAppConfig() {
        return appConfig;
    }

}

0 个答案:

没有答案