The need for simplicity
What do you want to see from an android app? It should be simple, convenient, fast and smart! Unfortunately, it’s not unusual for these elements to get lost under a lot of bulky code, which the developer implements to try and anticipate the user’s actions at every point in time and place. As Mozart supposedly said though, “true genius lies in simplicity”.
Setting the scene
Let’s imagine, that we’re working on a very simple social network application with all the usual attributes: a news feed, messages, favorites, groups. Typically, the interface of an application like this is saturated with a whole range of information, which is always changing (new messages, new events in the feed, friend requests and so on). That’s why we need to anticipate information changes, so that as they come our application responds to them quickly.
The answer in brief
So, to ensure our application is both intelligent and convenient for the developer:
- The http server should send push-notifications to the application in reaction to actions by other users (API calls);
- The application should receive and process push-notifications according to the events that caused them;
- The result of this processing should then influence or not influence the behaviour of the application.
How do we do that?
When an application receives a push-notification, we respond to it according to the event which happened (doing API calls, storing it in cache, DB etc). Then when we have all the data and have calculated the next steps, we send these guidelines to the application as Broadcast-messages.
But how should the application react to these messages? To refresh the UI, create a simple UpdaterInterface interface using the updateUi method. Create an abstract class ParentActivity, that will follow from the Activity. In the life cycle of this class we will act on the messages sent by the push-notifications which come to us from the server.
Some points to remember
Now we have everything we need to work with our application. We need to remember that firstly, every new Activity must inherit from Parent Activity; secondly, we need to write the implementation of the updateUI method every time and it must include the full data acquisition cycle (reading from cache, DB, API etc) and it’s reflection in the UI. Working with Fragments is built on a simple principle: if the updateUI method has been called, you just have to update the current fragment, rebuild it, or write it it’s own updateUI implementation.
Success through simplicity
Using this basic technique, any part of our application can gain a new lease of life, becoming independent, interactive and friendly to both the user and the developer! Enjoy!