One Activity and many Fragment, best practices?

Hello! Please advise a good solution to build proper architecture of the application with one activity and many fragments. Now I have an app with 20 screens. Each screen is implemented using fragment. All the pieces are in the same activiti. Unfortunately, the code in this activity became difficult to maintain, because the number of lines exceeds 4000. Basically the code is responsible for working with fragments, the UI, work with simple data. I understand that this should not be, but do not represent as it is possible to decompose.

Tell me best practices in this matter.
July 2nd 19 at 16:56
4 answers
July 2nd 19 at 16:58
I'd abandoned architecture "is One Activity for all the application" and did all the usual Activity.
The problem, apparently, is to break the application functions into loosely coupled modules. Razreshite and sign your screens and functions that belong to each of them, and think about which features are independent from each other, and which are not.
Read about the SOLID principles that help to understand how best to share responsibility classes between each other:
Separate the business logic from the UI logic and data storage logic with architecture Clean:
Organize communication UI and business logic using the MVP approach, where Model is the UseCases (they are also Interactors) of the Clean:
Try Moxy ( as a library to implement the MVP, it will save you from writing large amounts of boilerplate code and some of the problems with the coup of the screen.
July 2nd 19 at 17:00
Strangely, my activity is almost nothing but a method pushFragment(BaseFragment fragment).
All fragments are inherited from BaseFragment, which is a method getMainActivity.

And you can do so from any part getMainActivity().pushFragment(SomeFragment.newInstance())

Approach overall as in the iOS (read about uinavigationcontroller)
July 2nd 19 at 17:02
For each piece to create your class.
And you can use the structure of the architecture - MVC (Model-View-Controller)
Each fragment I have a class. If a portion of the methods, some fragments are repeated, then I have them stand in a separate class, the Ambassador of which other fragments are derived from it. I understand how to do it, but here's how to make a part of the functionality of activiti not yet understand. Now here I read this article and probably it is necessary to better understand the architecture of the MVP. How do you feel about the proposed article to solve the problem? - sigmund76 commented on July 2nd 19 at 17:05
I few months ago I read this article. In principle, the approach I like, and I'm trying all this on their projects. But so far, I have not a single campaign has not caught on because of my stupid habit of coding)) - beverly.Purdy65 commented on July 2nd 19 at 17:08
July 2nd 19 at 17:04
Wow!! 4000 lines - there is clearly something wrong. You need to break. UI logic should be in snippets, classes, vyuha, adapters, etc ... In the host activity, you should have the logic of the shift elements (although here it is also possible to distinguish separate classes). Business the logic allocate in a separate layer. 4000 lines - it's terrible!

Find more questions by tags JavaAndroid