I am writing an application that receives xml from a Web service and displays its contents in a ListView. Since java in general, and Android in particular, for me are new things, I have to recycle mountains of information, often outdated. The design patterns are also very superficially familiar. Now everything works for me, but there is no certainty that I am doing everything right. Please indicate what is wrong with my solution, and what design patterns I used.

My application consists of Application, Activity and Service. I had been reading about how badly to use AsyncTask and therefore decided to use the Services.

Application in onCreate creates a ServiceHelper and provides access to it. An interface is connected to the Activity, the response listener from the service. When you click a button in the Activity, ServiceHelper creates an intent and starts the service. After the service is completed, the service sends the result of the work through the ServiceCallbackListener interface in the bundle.

An event on the onServiceCallback event reads the result from the bundle.

Then I realized that in the case when the Callback from the service comes to a stopped application, the result goes to nowhere.

To solve this problem, I made it so that the service saves to the ServiceHelper bundle with the result of the work, and the Activity, after processing the callback, informs the helper that the cache can be cleared. Now I have an Activity, by the onPostResume event asking ServiceHelper if the service still processes the request with the specified id, if not, if there is no data for this id, and if so, loads them into the ListView and informs the helper that the data with that id need to remove.

Everything is good, but this implementation is inspired by the article https://habrahabr.ru/post/144275/ , which is already five years old. I heard that now in Android they use Loaders, retrofit, rxJava, and many other scary words, from which doubts arose if I don’t take bicycles to use ready-made objects?

  • I apologize for the offtopic and do not give a link to the article? - plesser
  • one
    What does not suit you in your current implementation? What does not work (for example, the screen turns bad / wrong / suboptimal / not supported at all)? Why do you need to build retrofits, rx, etc.? What profit do you expect? The use of technology for the sake of technology does not make sense. - Yura Ivanov
  • one
    At the moment everything works, if something does not work, I have not noticed it yet. You see, since I am at the stage of study, I would like to immediately learn how to do things right, and not to retrain later, and since there are no specialists in my circle with whom you can discuss this issue, I turned here. To criticize me and poked his nose what to teach. - Sergey Vodakov
  • one
    From experience I can say that these scary words work fine on hellow worlds and pet projects. If there is any unobvious case, it turns out not a profit, but a headache. There are too many restrictions on the life cycle, on the amount of memory, on processor time and many technologies that people of the java-world with their enterprises and unlimited resources go sideways in android. I'm not saying that all these terrible words are bad, but it will take quite a lot of time to study these technologies and use them in your project. No silver bullet. - Yura Ivanov
  • one
    As a constructive read frogermcs.imtqy.com articles about Dagger 2, there and other frameworks / technologies are used. And pay attention to the comments that there are problems and how they manage or cannot be solved. - Yura Ivanov

1 answer 1

Service it makes sense to use only if the exchange of information is ongoing, including when the application is turned off, if only at the moment when the application works much better, it is easier and simpler to restrict to ordinary Thread .

ListView thing is already obsolete, if you don’t write under Android 2.3, then RecyclerView used. All the same, it works faster, ViewHolder in the box and features more.

Retrofit a wonderful thing, but if you do not use a full-fledged good REST API, but instead just dump the data with a regular post, then it is better to limit OkHttp to the OkHttp library. If you want to convert from JSON large data on objects there is a GSON library to simplify the process, which is also not always necessary.

In the data bundles store in my opinion there is no need. You can send directly from the stream / service to the fragment / activation.

  • “There is no need to store data in bandles in my opinion. You can transfer directly from the stream / service to the fragment / activation. ”I transmit directly, but what to do if the recipient activates, at the time of receiving the data is asleep, paused or completely destroyed? - Sergey Vodakov September
  • Check it for null, you can track the state of the activation, with this somehow there is no problem. It is much easier to update the data. - DEADMC
  • Well checked, convinced. Where to put the data? Right now I put them in a bundle, when the activator wakes up, it will ask if there is no data for it, and will receive this bundle. - Sergey Vodakov September
  • OK. And then I knocked out the service and killed the application. And where is the data? In this case, if the data is very small SharedPreferences, if a lot of SQLite - DEADMC
  • In general, SQLite is usually used for such cases, otherwise if the user is without the Internet, then there is nothing to display at all, otherwise he will be able to do something with old data. - DEADMC