K
K
knott2012-10-12 19:00:03
Android
knott, 2012-10-12 19:00:03

Activities, Fragments, Particles...?

With the advent of fragments, it has become somewhat easier to separate the code.
But what if the fragment code grows into god-objects?
I solve the problem like this: I
group the views by functionality (Say, element selection and a button for creating a new one) and put them in a separate class (Conventionally calling it a particle), in which I resolve elements and do other dirty work.
But here the problem arises of forwarding callback calls from the hosting component: a fragment or an activity directly ( say , after creating a new element called startActivityForResult, you can get the result only inonActivityResulthost component, and you have to forward the data to the child component).
How do you solve such a problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VasiliyRylov, 2012-10-12
@VasiliyRylov

If I understand the problem correctly, then in this case, the Listener pattern is convenient for data transfer.
You just need to pay attention that in case of changing the position of the screen or other action leading to the re-initialization of the fragment, the listeners must be set again.
But, if the fragment exists in a single copy, the listener is defined as static and when the fragment is restarted, it does not fall off.

A
ara89, 2012-11-12
@ara89

In cases where the number of such Listener interfaces grows strongly, I recommend Otto Event Bus

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question