In android apps, we mostly use threads,other than main UI thread,when we are performing network operations . I have seen core Java applications using several threads at same time. So will it be a good practice to use multiple threads in Android other than network operations ? One of the reasons I feel that if I am using several threads and they are directly impacting the flow of my application then it may cause data mismatch as one of the thread may not have executed by the time Ui updates or user traverses to next activity. So all the data operations should happen synchronously. So basically is it a good practice to use multiple threads in android like pure java applications ? Also why this conditions does not occur in java applications ? Is is because of lower ability of the mobile to handle multiple processes as compared to a proper machine ? Thank you.
-
1Yes. That's normal. Use them – Vyacheslav Oct 23 '16 at 12:19
-
@Vyacheslav so lets us say I am working on a method where large chunk of data is being updated in arraylist ,and it is going to affect UI, I must use it in separate thread? What if user performs some other operation or traverses to next activity? – Pritish Oct 23 '16 at 12:36
-
@Pritish, you can't change the UI from another thread except the main thread. If you what you are trying to implement doesn't change the UI you can use AsynchTask that would run in the background and would notify the main thread when it will be finished. However, in your example you want to update the UI so either I would write my code under the main thread or I would use activity.runOnUiThread - which is a handler instance that will help you achieve your request. – Yzgeav Zohar Oct 23 '16 at 12:44
-
Each networking thread must work not inside ui thread. Otherwise, it handlers an error – Vyacheslav Oct 23 '16 at 12:56
-
You have to handle applicayion behaviour if iser changes Activity. The other created threads do not be killed if user opens new activity – Vyacheslav Oct 23 '16 at 12:58
-
1*"why this conditions does not occur in java applications?"* Sorry to disagree. They do happen. A lot. – m0skit0 Oct 23 '16 at 14:22
-
@m0skit0 As I am not core java developer I don't know that exactly what kind of real time complexities are encountered there , but if they do happen there too as you said, then it answers my question that I should be using them only if required in case of android ,because here I have to maintain hell lot of things like activities across stacks, fragments state of applications etc .and it will unnecessarily complicate stuff much more. – Pritish Oct 23 '16 at 14:51
-
Adding value to your software is not something obvious and easy, and tha's why not all software is equal in performance and overall quality. – m0skit0 Oct 23 '16 at 18:13
1 Answers
You are asking the wrong question: the real question should be: how do I design my Android app to give the user the best user experience?
As a programmer, you don't use concepts because they exist, but because they add value to the things you are doing.
For example, you look into threads to ensure that your app doesn't block or becomes unresponsive. If your app needs to do many different things, then it makes a lot of sense that these things can happen in "parallel" to a certain degree.
Of course, threads add complexity to your logic; and of course, when you start using threads, then you need to know what you are doing. Using multiple threads that cause data mismatch per se.
What causes problems is when you do not know what you are doing!
Long story shorts: your question sounds more like I am not sure how to do multi-threaded programming on Android; and thus you should study that topic; starting here for example!
Edit: In the end, this is about balancing and being pragmatic. On the one hand, yes, (correct!) multi-threading adds value to your application; but of or course, there is also cost in terms of additional complexity. So the essential answer is probably: do what you feel comfortable with. If you have the feeling that multi-threading makes it harder for you to deliver a product that works correctly, then that answers your question - you should not use things that you are not comfortable with!
-
Though you may be correct about the question part, that is, yes it is related to whether I can improve user experience by using threads,but actually I am not sure if I should be doing it, because even minute mismatch creates problems in applications .For example in my last last application I was traversing to a child fragment in viewpager on click of notification , but many times just because some UI method would get called before it the fragment got properly attached, my app would crash .so I had to implement isadded() to many methods . – Pritish Oct 23 '16 at 13:01
-