1

The answer to this question would be a design-pattern, to be used in development of Android applications, that simplifies the way user interface elements are managed such that many such elements can be managed centrally even though acted upon from various places in the application.

Given a small application without too many states, managing UI elements in the activity life cycle methods is fine. If the application grows and can change states based on processes outside the main thread, the number of places where changes are being made increases. This makes the application less maintainable (having state changing code in many places). I seek a design pattern that makes it more clear what happens to the UI in the various states.

I realize that persistence tools such as SQLiteDatabase andSharedPreferences are available, which might be part of the design pattern, but central control, where the state of the app can be maintained, along with control over what the user sees and what the user is able to do is the goal.

Dale
  • 5,520
  • 4
  • 43
  • 79
  • 1
    This question is probably *way* to broad. You should have a look at the MVP pattern, and an there is an [official android course](https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#0) – David Medenjak May 26 '16 at 19:24
  • By 'states', imagine an app that can be logged in or not, can be connected to database1 or not, connected to database2 or not, all the while providing various sets of services (and associated UI controls), depending on which of those five states matches the current state. – Dale May 26 '16 at 19:25
  • Maybe the answer is MVP? Implementation might be a challenge since UI and data access are naturally tightly coupled in Android activities. – Dale May 26 '16 at 19:34
  • Now put all this logic into some service layer, and access that layer with your presenter. Perfectly testable. – David Medenjak May 26 '16 at 19:35
  • @DavidMedenjak, I can't accept that the question is too broad since it's asking for a design pattern that works in complex Android apps to simplify managing the view. – Dale May 26 '16 at 19:37

1 Answers1

1

There is a pattern called "MVP" (Model / View / Presenter) that has been used for this purpose. This answer provides reasons why MVP is preferred over the MVC triad when programming in the Android framework.

Taking a queue from this article, we see that the idea behind this pattern is to separate the model and view with the presenter.

  • The Presenter - Like the MVC pattern, the presenter gets data from the model and returns it to the view. Additionally it also decides what happens when you interact with the view.

  • The View - This will be an activity and will create the presenter. It will listen for activity, but instead of doing anything itself, it will request that the presenter take the action.

  • The Model - The provider of data that we wish to display.

There is an example of a working MVP application androidmvp on github

Dale
  • 5,520
  • 4
  • 43
  • 79
  • So after rewriting an app that was kind of out of control, I'd say this works. There IS a bunch of what I'd call vertical repetitition, where one layer doesn't seem to be adding much value, but it's more like something I'd admit to writing – Dale Jun 15 '16 at 01:38