S
S
Sergey Ivanchenko2016-08-01 16:15:37
Android
Sergey Ivanchenko, 2016-08-01 16:15:37

How to separate Android Activity or fragments and application state?

In our android projects, the transition between fragments or activities through intent is practiced, in which one activity calls another or one fragment initiates the launch of another fragment. As a result, each fragment must know about the next fragment and the data that it must transfer.
I want to remake this architecture so that there is only an object that stores the current state of the user, for example, what form he is on, his data, so that the object saves its state during onPause and so that fragments or activities can always be built from this object even if the application is minimized or closed. And so that fragments or activities do not know anything about other forms.
Are there examples of such an architecture?
Or frameworks that already follow this methodology?
Or how do you build such an architecture?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Yakushev, 2016-08-11
@mstr

I use a state machine for this :

  1. We create a wrapper for the state machine, into which it is possible to pass an event. Thinking about saving and restoring state
  2. We use Singleton or Dependecy Injection to get a reference to our wrapper
  3. Write the states and events of the state machine
  4. We create an interface for subscribing to the state machine, which has a method that returns a fragment or intent
  5. Each fragment or activity inherits from the base one, which implements the subscriber interface. We start the incoming intent or fragment in the handlers
  6. In the activity itself and the fragment, we send events to the state machine

A
Alex Kisel, 2017-09-28
@ziginsider

If you need to be able to access data and still be independent of life cycles, you can try the default solution from Google: Android Architecture Components . Pretty simple to use. Presentation with Russian subtitles: https://youtu.be/Dl4DiQRxBi4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question