0

I am confusing about normal threading and service. I am doing a form application which sending data to the server on each activity. Some area may need to upload images.

If I am using normal thread to send the data to server and at the same time I may have a progress bar showing on the screen. During uploading, user may decide to do something else before coming back to the same form of the application. Will the activity be destroyed when user when off using other application while the phone in low memory state? if so, the activity of my application will re-create when it comes back to the foreground. Then the progress bar supposed to be visible while still uploading will be gone.

Or am I going to the wrong direction.

LittleFunny
  • 8,155
  • 15
  • 87
  • 198

1 Answers1

0

In your case(form submission) Asynctask seems a better candidate. Asynctask is sophisticated version of thread with a lifecycle. lifecycle methods are useful in tasks such as showing a progressbar.

Threads should be avoided in Android in favour of asynctask.

however if its a long running operation Service is recomanded way.

also have a look here

Community
  • 1
  • 1
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • I use volley to do the networking so do i still need the Asynctask... I think I will use service to upload image coz I don't know how long the uploading will take – LittleFunny Oct 24 '15 at 07:31
  • yes, for uploading large data you should use service. volley does threading internally so you need not to use thread or asynctask with it – Rahul Tiwari Oct 24 '15 at 09:55
  • When the service ends, will the thread which running inside this service also get stopped or continuously running. – LittleFunny Oct 26 '15 at 08:24
  • That thread will be stopped when service will end. and anyways you need not to care about threading. – Rahul Tiwari Oct 26 '15 at 08:34
  • Some people in stackoverflow saying Service should be used for long term running task such as playing music, or monitor data changes etc. This phrase make me confusing. If I use volley by itself without service, will there be any disadvantage. – LittleFunny Oct 26 '15 at 08:51
  • It totally depends on how long your operation is. uploading large data can take some time and specially when you dont want to take risk with uploading large image file when your app is in background you should be using service. – Rahul Tiwari Oct 26 '15 at 09:05
  • Ok iI see.. You said app is in background, isn't thread or asynctask also running when app is in background – LittleFunny Oct 26 '15 at 09:10
  • yes they will run but there is no guarantee that they will run to completion when app is in background. OS may kill them to reclaim the resources as your app is in background. As per my observation if operation takes 2 to 10 seconds it may run to completion using thread or asynctask but this will change from device to device. – Rahul Tiwari Oct 26 '15 at 09:22