Вопрос

I have an android activity that needs to run six asynctask classes. I created my activity and it works fine, but with those 6 classes my code is so hard to comprehend or edit. Maybe its dump but i am new in android so what i need is to know how to simplify my code a bit: is there a possibility to make all async tasks in one class knowing that they have different functionalities

Это было полезно?

Решение

You can have one Async Task only and do different things in it by passing a functionality ID, if you may, as a parameter.

Although, I strongly recommend you to create 6 different files, each containing your Async Task class (if they have truly different functionality) and put them in a package "Async Tasks", if you may.

If, for instance, you need to do different sort of requests to a server, you should only have one Async Task, different parameters (such as URL, post parameters, etc) and handle the result different in your onPostExecute method according to the type of request you have done: send an event to your activity that calls the proper method to handle the result.

You can use this technique to manage you more-general Async Tasks

Другие советы

is there a possibility to make all async tasks in one class

Yes, but don't. You mention "simplifying" your code - putting more functionality into one class will make it difficult to understand what that class is responsible for.

The better option would be to add 6 different implementations of AsyncTask, and create new instances of them in your Activity. The names you use for the classes should represent what they do, and they should do as little as possible.

You could move your AsyncTasks to separate files. You could pass the Activity to your AsyncTasks constructors.

Might be you also could combine some of your tasks into one.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top