Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
My professor told us in the class that updating a UI element from a background task is a bad practice because it might hang our UI, I can't understand why, can someone please elaborate
Background threads are not aware of your ui state , in simple words they are not aware if your ui still exists or not , in this case if you try to update your ui from the background thread and if by any chance your ui element doen't exist at that time, your app will misbehave (unexpected behaviour) or CRASH .
So to avoid this unexpected behaviour, we only update the ui from the main/ui thread.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Whats the threshold to move code execution to a background thread (asynctask, services, threads etcc) in Android ?
Say if we know a task will complete in 50 milliseconds, should we offload it to background ?
What is the limit ?
The official documentation Keep your app responsive says:
Generally, 100 to 200ms is the threshold beyond which users will
perceive slowness in an application.
But it also depends on what you are doing on the screen. The screen is refresh at 60Hz (16.6ms), which can be a problem if you are playing an animation.
They also gives many advices to fix this issues on the ANR Documentation
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Is it a good practice to make long tasks such as server requests in the application class? Let's say these requests are for initialization,is it still fine to place these requests in the oncreate method in the Application class.
It is not. Everything inside the onCreate of Application class will be executed in the main thread, resulting in freezing the UI if your task takes a lot of time.
The best practice, when it comes to operations such as communicating witha server, is to implement a Repository Pattern and execute the time consuming operations in a different thread, then use the results in the UI Thread.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
In which situation is it acceptable to make network calls on the UI thread? Or we can say that Network calls should never be on the main UI thread.
Straight from Android documentation:
Perform Network Operations on a Separate Thread
To avoid creating an unresponsive UI, don't perform network operations on the UI thread. By default, Android 3.0 (API level 11) and higher requires you to perform network operations on a thread other than the main UI thread; if you don't, a NetworkOnMainThreadException is thrown.
It is never acceptable to make network calls on the UI thread.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why we call android activity visual representation of android app when it also does background tasks which have nothing to do with visual representation.
A lot of programmers consider it a bad programming style to write code for background tasks into an Activity class. When aiming towards a clean architecture one would ideally only find representational code in activities. Thus, updating the views that make up the activity. As your question implies, background tasks are better found in separate classes that make up the business logic layer of an Android app.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
can you explain why does not allow network access in the main thread of a process in Android?
If anybody can explain. Its will be helpful for me.
Because the main thread is responsible for all the UI (user interface) operations (it is also called the UI thread). Everything related to displaying stuff in the screen is done by it. So if you occupy the main thread with long operations such as a network operation, you will experience jitter in the screen and even ANR errors (Application Not Responding). In more recent versions of Android you will not even be allowed to do that as the application throws NetworkOnMainThreadException.
Till Donut it is used to create multiple threads from 1.6-2.3 that you was using before but if now you are using 3.0 or above then it is used to create single threaded model by using AsyncTask otherwise you ll get the NetworkOnMainThreadException.