I create copy of this post here because this place more relevant for my question and I don't remove original question from stakOverFlow because I got comments and start conversations with users.

I often meet in my code or in code from colleagues with bunch of init methods in onCreate() method in Activity and it's looks like this sample:

onCreate() {

private void initUI() {

That's what bothers me here:

  • Sometimes order of methods calls is important and change herу lead to crashes.
  • Make all work with UI in one methods lead to code sheets with bad readability.
  • The methods in code looks scattered, chaotic and unrelated. We can accidentally skip one method-call in initUI() method and we will get bug.

What you doing with this problem?


If your system is composed of a lot parts you have to initialize them. And if they do not fall into any reasonable nested hierarchy, but really are a long list of parts, then the correct way to represent them in code... is a long list of parts. It seems to me that this is what you are already doing.

Not every method over five lines has to be refactored. In this case the organization of the init code seems to reflect the structure of the system accurately, and that's what matters. If the system is refactored one day so that it consists of fewer, semi-independent modules with a few parts each, then of course the init code would change to match; but you shouldn't make the constructor make the system look neater than it actually is.


Consider creating custom components like DistanceView or ConditionsView which themselves perform all the initialization for these components. Then you need only include these components to your application and they will initialize themselves. Although you may be using these components once, it still offers a cleaner solution, as well as allows you to potentially adapt said component later for events or particular methods for performing tasks specific to that component.

Another trick you could use is to round up tasks which may often occur in many of your components and create a base class which performs these tasks instead.

The result is freeing up your Activity class to perform only tasks which are neither generic (handled by base class) nor specific to any one component (handled by custom components).


You could use something like Dagger2 to inject atleast most of the components(other than views).
Here is a good tutorial: https://medium.com/@harivigneshjayapalan/dagger-2-for-android-beginners-introduction-be6580cb3edb

  • When your answer is mostly a link, try to rephrase your linked content to prevent link rot. – coteyr Jan 22 '18 at 9:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.