传递活动于非活动对象正确 [英] Passing Activity to non-activity object properly

查看:92
本文介绍了传递活动于非活动对象正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序香港专业教育学院有一个非活动对象,它的作用是作为一个管理器类。 很多时候我需要通过源活动的方法作为参数传递给该管理器类,以该活动使一些操作。

In my app ive got a non-activity object which it's role is being a manager class. many times i need to pass "source activity" to methods as parameter to that manager class in order to make some operations on that activity.

例如假设我们有MyActivity这得做一些烤面包。 所以我有一个名为MyManager这个管理器类,和我有这个方法,它

for example let's assume we have MyActivity which gotta do some toast. so i have this manager class called MyManager, and i have this method in it

raiseToast(Activity sourceActivity) {

  Toast.makeText(sourceActivity, demo, Toast.LENGTH_LONG).show();
}

和从Myactivity类中,我们调用该方法是这样的:

and from Myactivity class we calling that method this way:

MyManager manager=new MyManager();
manager.raiseToast(MyActivity.this);

它工作正常。

it works fine.

我在问什么在这里,这是一个活动作为参数传递给非活动对象以适当的方式? 我在真实设备(不是仿真器)一个内存泄漏,我不知道是否这也可能引起的任何原因是什么?

what I'm asking here, is this a proper way to pass an Activity as parameter to a non-activity object? I'm having a memory leaks on real device(not the emulator), I wonder if this could also causing any reason for that?

由于伊詹。

推荐答案

您可以尝试通过应用程序上下文是getApplicationContext()上的活动。 为什么你有这样的MyManager对象?你可以只从提高敬酒的活动,而无需它单独的类。将你的方法raiseToast()来活动身体,只需要调用它。

You may try to pass application context which is getApplicationContext() on activity. Why do you have this MyManager object ? You can just raise toast from activity without having it in separate class. Move your method raiseToast() to activity body and just call it.

编辑:请阅读的http:// Android的开发者。 blogspot.com/2009/01/avoiding-memory-leaks.html

这篇关于传递活动于非活动对象正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆