views:

352

answers:

1

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.

for exaple 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();
}

and from Myactivity class we calling that method this way:

MyManager manager=new MyManager();
manager.raitetoast(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?

Thanks Idan.

+1  A: 

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.

EDIT: please read http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

Alex Volovoy
Yes ofcourse in this case it pretty much silly, i jus gave an example when i need to pass the current activity to some non-activity object, in this case it's realy useless, but in other cases you might just need to seperate the inside logic from the gui(MVC)so how would you do it exaclly in this way of getApplicationContext() ?Thanks
rayman
How should i send the paramet from the calling Activity, and how should i set the parameter in the manager class?Thanks.
rayman
getApplicationContext() is context method. So you can replace manager.raitetoast(MyActivity.this); with manager.raitetoast(getApplicationContext());
Alex Volovoy
10x, but how exaclly this might reduce leaks?
rayman
If you're leaking your object you might leak whole activity with it.Please read this article in url i've posted to the answer
Alex Volovoy