jeudi 11 octobre 2018

Android - Change framelayout fragment from the fragment itself. Using a viewmodel

Well i was working on my app. I want to use one activity that behaves like a holder activity for my fragments. Now i wanted to change the fragment from the fragment itself using viewmodel. I share the viewmodel with the activity.

Now my question is. What could be the best way to change the fragment using a viewmodel.

  1. Is it using states that could be observed and a big switch statement in your activity class (Not preferred).
  2. Passing a fragment object into the viewmodel that can be observed. (is this not violating the whole principle of the viewmodel, because a fragment has a lifecycle?)
  3. Mixture of using a interface for chaning the fragment. Viewmodel to send the data between the activity/fragments)

Maybe this approach is just not the best way?! Why i choose for this way. I don't want to start a new activity for every little (with opening animation) step in the setup process in my app.

Thanks!

Greetings,

Ian

Aucun commentaire:

Enregistrer un commentaire