The problem is with a non-static inner class is that you don't 'see' the 
implicit reference to the outer instance (OuterClass.this). It is easy to 
forget about this one and later realize what problems it causes.

Usually, 
when the lifetime cycle of an inner class matches the lifecycle of the 
outer class (e.g. OnClickListeners,etc, or BroadCastReceivers that are 
unregistered in the (outer) Activity's onDestroy), I make it non-static.
When the lifetime cycle of an inner class does *not *match the lifecycle of 
the outer class, I make it static. If a reference to the outer class' 
instance is needed, I either 'wrap' it in a WeakReference or create 
specific methods for detaching the outer instance from the inner instance.

On Monday, March 9, 2009 10:17:01 PM UTC-4, Greg Krimer wrote:
>
> 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


On Monday, March 9, 2009 10:17:01 PM UTC-4, Greg Krimer wrote:
>
> 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


On Monday, March 9, 2009 10:17:01 PM UTC-4, Greg Krimer wrote:
>
> 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


On Monday, March 9, 2009 10:17:01 PM UTC-4, Greg Krimer wrote:
>
> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to