更好地加载在运行或pre资源加载它们的内存

更好地加载在运行或pre资源加载它们的内存

本文介绍了更好地加载在运行或pre资源加载它们的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发跨几类跨越一个应用程序,正因为如此,我不得不通过对应用程序的语境下对所有使用全局字符串或整数类,以便从strings.xml中提取它们或dimens.xml文件,而不是硬编码他们。到现在为止,我已经加载一次所需要的资源,但我不知道是否会更有效地加载,我需要在构造函数的类的所有资源,记住,他们只是字符串和整数值。

I'm developing an app that spans across several classes, and as such, I've had to pass on the app's Context to all of the classes that use global strings or integers so as to extract them from the strings.xml or dimens.xml files instead of hard coding them. Up until now I've been loading the resources once needed but I was wondering if it would be more efficient to load all the resources I would need on a class in the Constructor, keep in mind that they're only strings and integer values.

推荐答案

有关的小物件(字符串,数字),您可以preLOAD他们,因为它不会花费太多的记忆。

For small Objects (Strings, Numbers) you can preload them as it wouldn't take much memory.

有关大对象(图像,媒体),最好是根据需要加载。

For big Objects (Images,Media) it is better to load as needed.

有关的小物件我个人的preference是延迟加载它们(负荷需要的,但加载后保存在内存中,供以后使用),是这样的:

My personal preference for small Objects is to lazy load them (load as needed but after loading keep in memory for later use), something like:

public class Values{
     private static Object pref1;

    public static Object getPreference1(){
         if(pref1 == null){
             pref1 = heavyLoadingOfPref1();
         }
         return pref1;
    }
}

由于,发现我使用它的各种小物件,弦乐(尤其是本地资源)它更多的是懒得加载它们这个样子,只是在开始时(如果你定义了他们的话,那就更加载它们一次可能他们会被使用)。

As, noticed I use it for sorts of small objects, for Strings (especially local Resources) it is more of a bother to load them like this, just load them once at begining (if you have defined them then it is more likely they will be used).

这篇关于更好地加载在运行或pre资源加载它们的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:04