2

Android Studio provides good refactoring items. But when to do "convert anonymous to inner" is better?

For Example:

        new Thread(new Runnable() {
            @Override
            public void run() {
                // do something
            }
        })

After Refactoring:

         new Thread(new MyRunnable());

         private static class MyRunnable implements Runnable {
             @Override
             public void run() {
                 // do something
             }
         }
SilverCorvus
  • 2,956
  • 1
  • 15
  • 26
pop
  • 105
  • 1
  • 7
  • the only difference is when inner is static ... in this case it is not store reference to outer class ... I'm pretty sure that if you would use some method/field from outer class in `run` it will generate not static class ... on the other hand you can reuse such class (of course it will not apply if you used it only once) – Selvin Oct 20 '15 at 08:26

2 Answers2

1

It is much easier to create several instances of an inner class (by calling its' constructor) than creating several instances of an anonymous class.

A much easier question to answer is How are Anonymous (inner) classes used in Java?, which in short is to either quickly implement an interface (or abstract class) without adding much functionality to the interface (or abstract class) vs. wanting a strongly typed sub-class of another class which which will be constructed regularly.

A good example for interfaces/abstract classes you'd usually want to use an anonymous inner class are Runnables and most Listener interfaces, since they only serve as a wrapper for a piece of code "given" to an instance of another class. Good examples for inner classes are Fragments in android or custom controls (View in android), though refactoring those into separate classes can make them much more reusable.

A quick and dirty test is "Do I need create my own constructor for my inner class?", if the answer is "yes" than use a non-anonymous inner class.

Community
  • 1
  • 1
SilverCorvus
  • 2,956
  • 1
  • 15
  • 26
1

As far as I know, The static method you should use to avoid Memory Leak. Eg:

new Thread(new Runnable() {
        @Override
        public void run() {
            // do something long here
            YourActivity.this.doSomething();
        }
    })

--> above code will make your app get memory leak error when you close your app when the thread is running. However, with new refactor you can avoid memory leak easily:

 private static class MyRunnable implements Runnable {
         WeakReference<YourActivity> activity;
         @Override
         public void run() {
             // do something long
              if(activity.get() != null){
                  activity.get().doSomething();             
              }
         }
     }
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
  • Wouldn't it be better to simply synchronize the thread with the activity life cycle? In my opinion, a thread that is tied to an activity should not be running once that activity's instance leaves onDestroy (or even onPause, depending on what it does). – SilverCorvus Oct 20 '15 at 09:07
  • @CurlyCorvus yes, but you cant stop thread from outside when it's running. Then, thread still hold an instance of your activity till the thread finished --> memory leak still occurs. – Kingfisher Phuoc Oct 20 '15 at 09:21
  • http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java You can, and a good way to do it is in http://stackoverflow.com/questions/3696506/how-to-kill-a-thread-and-handler-before-going-to-new-activity. – SilverCorvus Oct 20 '15 at 09:39
  • @CurlyCorvus You should read carefully and check it yourself. Just check it with Thread.Sleep inside your runnable and see the result. – Kingfisher Phuoc Oct 20 '15 at 09:46