处理资源的最佳方式

发布于 2024-10-05 16:43:24 字数 1451 浏览 4 评论 0原文

我在尝试将资源传递给我的活动中使用的子类时遇到了困难。我用两种方法解决了这个问题,但不确定一种或两种方法是否会导致可能的内存泄漏。所以这是我到目前为止所拥有的:

-myactivity (活动类)

-global (包的全局类,我用来保存全局可访问变量)

-subclass (我想在其中使用可绘制资源的子类)

a)

public class global{
    public static Resources appRes;
    ....
}

public class myactivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        global.resApp = this.getResources();
        ...
    }

    private void somewhere(){
        subclass tmp = new subclass();
        tmp.subclasmethod();
    }
}

public class subclass{
    public subclass(){...}

    public void subclassmethod(){
        Bitmap bmp = BitmapFactory.decodeResource(Global.appRes, R.drawable.myres);
        ...
    }
}

b)

public class myactivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    private void somewhere(){
        subclass tmp = new subclass(this.getContext());
        tmp.subclasmethod();
    }
}

public class subclass{
    Context context;

    public subclass(Context context){
        this.context = context
        ...
    }

    public void subclassmethod(){
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myres);
        ...
    }
}

预先感谢您的反馈。

I was kind of stuck trying to pass the resources to a subclass used on my Activity. I solved it in two ways, but not sure if one or both will lead to possible memory leaks. So here is what I have so far:

-myactivity (the activity class)

-global (global class to the package, I'm using to to save global accesible variables)

-subclass (the subclass where I want to use a drawable resource)

a)

public class global{
    public static Resources appRes;
    ....
}

public class myactivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        global.resApp = this.getResources();
        ...
    }

    private void somewhere(){
        subclass tmp = new subclass();
        tmp.subclasmethod();
    }
}

public class subclass{
    public subclass(){...}

    public void subclassmethod(){
        Bitmap bmp = BitmapFactory.decodeResource(Global.appRes, R.drawable.myres);
        ...
    }
}

b)

public class myactivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    private void somewhere(){
        subclass tmp = new subclass(this.getContext());
        tmp.subclasmethod();
    }
}

public class subclass{
    Context context;

    public subclass(Context context){
        this.context = context
        ...
    }

    public void subclassmethod(){
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myres);
        ...
    }
}

Thanks in advance for you feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-10-12 16:43:24

如果您希望全局类存储应用程序范围的值,您至少不应该使用选项a。相反,请查看 Application类,旨在帮助您解决以下问题:

为那些需要的人提供基础课程
维护全局应用程序状态。

否则,您在选项 b 中建议的替代方案是一种不错的方法。至少如果您需要的只是传递对应用程序上下文的引用,以便您可以访问资源。

If you want a global class to store application-wide values, you should at least not use your option a. Instead, take a look at the Application class, which is meant to help you with exactly this:

Base class for those who need to
maintain global application state.

Otherwise, the alternative you suggest in option b is an OK way to do it. At least if all you need is to pass along a reference to your application context so that you can access the resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文