Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: AsyncTask recommendations: private class or public class?

I'm currently developing some Android Apps in a team and we've used 2 different approaches during the last months (one that i personally prefere, and the other that the other developer prefers).

Although so far the results are the same, this got me wondering...should we:

  • use AsyncTasks as private classes inside the Activities that use them.
  • or use AsyncTasks as separate public classes that receive the context of the activity

Are any of the approachs recommended by Google?

What does your experience say about this (advantages, disadvantages, problems)?

like image 523
neteinstein Avatar asked Jan 28 '11 01:01

neteinstein


People also ask

Why is Android AsyncTask deprecated?

This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

How does async task work in Android?

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.

What are the problems in AsyncTask?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.

What can I use instead of AsyncTask in Android?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

How do I use asynctask in Java?

To use Async Task, simply declare a new class, make it extend the AsyncTask class with the 3 parameters as discussed below. An asynchronous task is defined by 3 generic types, called Params, Progress and Result (here Integer, Integer and String respectively). Any class that extends AsyncTask has these parameters.

Is asynctask a threading framework?

This is quite similar to Threading but does not constitute any threading framework. Background tasks such as loading images, downloading music, pictures, files, etc can be performed using AsyncTask. However, AsyncTask is now deprecated and developers might sooner or later need an alternative for this.

Is asynctask now deprecated?

However, AsyncTask is now deprecated and developers might sooner or later need an alternative for this. Through this article, we will show you 2 methods through which you can perform background tasks and simultaneously update the UI elements.

How does an asynchronous task communicate with the activity on?

An asynchronous task is defined by a computation that runs in the background thread and whose result is published on the UI thread. But how does this class communicate with the activity this is put on? To use Async Task, simply declare a new class, make it extend the AsyncTask class with the 3 parameters as discussed below.


1 Answers

Inner classes are good for representing objects that are meant to be private or somehow intimately tied to the enclosing class. Occasionally there are technical reasons for using inner classes (e.g., simulating closures). They also cut down on namespace pollution.

One disadvantage of inner classes is that if they access private members (fields or functions) of the enclosing class, the compiler will generate accessor functions to those members. Language purists will argue whether this breaking of encapsulation is a Good Thing or a Bad Thing. The access functions add a bit of overhead to each access (which usually isn't a factor, but there it is). Another disadvantage is that it makes the source file more complex and therefore harder to manage. (I've occasionally been stung by editing a function in the inner class while thinking it was in the outer class, and vice versa.) Finally, inner classes tend not to be reusable, while separate classes can often be parameterized to have multiple uses.

These pros and cons are off the top of my head. I'm sure others will have additional thoughts.

UPDATE:

In this Google IO video the inner AsyncTask option is clearly marked as wrong option.

like image 168
Ted Hopp Avatar answered Sep 22 '22 01:09

Ted Hopp