Hello,
I have been finding it convenient to extend Handler in many of my
activities to handle messages specific to the activity. The handler
sublass is an inner class of the activity and needs to access its
state. I am wondering if there is any performance difference between
making the handler subclass static and passing in the activity
explicitly in its constructor or making the subclass an "instance
class" and letting the vm worry about my accessing members of the
containing activity.
The static approach:
public class MyActivity extends Activity {
private static final class MyHandler extends Handler {
final MyActivity mParentActivity;
public MyHandler(MyActivity parentActivity) {
mParentActivity = parentActivity;
}
public void handleMessage(Message msg) {
// access mParentActivity
}
}
}
The non-static approach (more concise!):
public class MyActivity extends Activity {
private final class Myhandler extends Handler {
public void handleMessage(Message msg) {
// access MyActivity.this
}
}
}
}
I read somewhere that non-static inner classes are to be avoided
because of the implicit reference to the containing class instance
that needs to be associated with the inner class. But in the static
example above I am just providing this reference explicitly. Also it
seems like non-static inner classes are used liberally in the Android
source code and examples. So maybe my fear of non-static inner classes
is unfounded?
(I have read the Designing for Performance best-practice doc and taken
its advice of using package scope with inner classes to heart. But I
don't think it quite answers my question -- all the more reason for
thinking that the distinction between static and non-static inner
classes is of no real significance?)
Thanks,
Greg
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---