E
E
Ernesto Guevara2015-04-08 10:28:06
Java
Ernesto Guevara, 2015-04-08 10:28:06

How to make one data model for multiple android fragments?

There is a model in the form of a large non-trivial object with a bunch of fields. Within an Activity, multiple fragments must work with this object. Very similar to the Observer pattern. Everywhere advice is given to use the fragment.setArguments(bundle) function to pass data to fragments via the API. But I don't understand why copy data and waste memory. What is wrong with the approach when we simply create a model in the Activity and pass in the constructor to each fragment a reference to this model?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FoxInSox, 2015-04-08
@guevara

The fact that the system can kill the fragment and recreate it. Of course, the system will pass nothing to the constructor.
The model for good should not be stored in RAM, but should, for example, be stored in the database. How to update and load data from the database is up to you.

C
Copperfield, 2015-04-08
@Copperfield

Look in the direction of MV* patterns. For example, MVP.

M
Marat S, 2015-04-13
@aratj

and if you look at the implementation of the fragment manager?
you will see that when restoring the state, it will pull an empty constructor, which is why it makes no sense to add your own constructor with parameters to the fragments.
either there will be an error, or nothing.
I don't have models at all. just observers on the database, something has changed in the database, it changes in other fragments.

D
dchuvasov, 2015-05-26
@dchuvasov

If you have one activity or data needs to be transferred only inside this activity, then you can use this trick:
1) create a model
2) create an empty fragment in the constructor, create a model, and in onCreate call the setRetainInstance(true) method; (this is to protect the fragment from destruction and recreate)

public class SignInWorkerFragment extends Fragment {
  private final SignInModel mSignInModel;

  public SignInWorkerFragment() {
    mSignInModel = new SignInModel();
  }

  @Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
  }

  public SignInModel getSignInModel() {
    return mSignInModel;
  }
}

3) we push this fragment into the fragment manager and set a tag for it, so that later we can extract it and get a link to our model
final SignInWorkerFragment retainedWorkerFragment =
        (SignInWorkerFragment) getFragmentManager().findFragmentByTag(TAG_WORKER);

    if (retainedWorkerFragment != null) {
      mSignInModel = retainedWorkerFragment.getSignInModel();
    } else {
      final SignInWorkerFragment workerFragment = new SignInWorkerFragment();

      getFragmentManager().beginTransaction()
          .add(workerFragment, TAG_WORKER)
          .commit();

      mSignInModel = workerFragment.getSignInModel();
    }

The whole project is here
And the story about the whole thing and more, in the course of the online school from e-Legion: video link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question