Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Activity to non-activity object properly

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

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?

Thanks Idan.

like image 739
rayman Avatar asked Feb 12 '10 16:02

rayman


1 Answers

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

like image 167
Alex Volovoy Avatar answered Oct 07 '22 23:10

Alex Volovoy