R
R
robert_n2016-11-14 16:33:10
Java
robert_n, 2016-11-14 16:33:10

One Activity and many Fragments, best practices?

Hello! Please advise a good solution for building the correct application architecture using one activity and many fragments. Right now I have an app with 20 screens. Each screen is implemented using a fragment. All fragments are in one activity. Unfortunately, the code in this activity has become difficult to maintain as the number of lines already exceeds 4000. Basically, the code is responsible for working with fragments, UI, working with simple data. I perfectly understand that this should not be, but so far I have no idea how it can be decomposed.
Prompt best practices in solving this issue.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ershov, 2016-11-24
@alaershov

If I were you, I would abandon the "One Activity for the entire application" architecture and do everything on regular Activities.
The problem you have, apparently, is breaking up the functions of the application into loosely coupled modules. Draw and describe your screens and the functions that belong to each of them, and consider which of the functions depend on each other and which do not.
Read about the SOLID principles that will help you understand how best to share the responsibility of classes between each other: https://realm.io/news/donn-felker-solid-part-1/
Separate business logic from UI logic and data storage logic with Clean Architecture: fernandocejas.com/2014/09/03/architecting-android-...
Connect UI and business logic with an MVP approach where Model is UseCases (aka Interactors) from Clean: antonioleiva.com/mvp-android
Try Moxy ( https://github.com/Arello-Mobile/Moxy) at as a library for implementing MVP, it will save you from writing a lot of boilerplate code and some of the problems with screen flips.

Z
z0rgoyok, 2016-11-15
@z0rgoyok

Strange, I have almost nothing in my activity except the pushFragment(BaseFragment fragment) method.
All fragments inherit from BaseFragment, which has a getMainActivity method.
And you can do this from any fragment getMainActivity().pushFragment(SomeFragment.newInstance())
Approach in general as in iOS (read about uinavigationcontroller)

V
Vadim Raksha, 2016-11-14
@hasdfa

Create a class for each fragment.
And you can use the architecture building - MVC (Model-View-Controller)
https://ru.m.wikipedia.org/wiki/Model-View-Controller

M
mitaichik, 2016-11-15
@mitaichik

Wow!! 4000 lines - there is clearly something wrong. You urgently need to break. The UI logic should be in fragments, view classes, adapters, etc. In the host activity, you should have the logic for changing fragments (although you can also select separate classes here). Separate business logic into a separate layer. 4000 lines is a horror!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question