".. it is unnecessary for a worker fragment to start a asynctask ..."
I don't understand the statement above.  A Fragment without a UI is not a 
worker thread. It is just a Fragment without a UI.

You can use it to tie data/tasks in the Fragment to the lifecycle of an 
activity that is hosting the Fragment:

- If you want your data/tasks to survive configuration changes (e.g. 
orientation changes) that may destroy and recreate the hosting activity:
Call 'setRetainInstance(true)' in the onCreate of your Fragment. 
Destroy/release/stop the data/task in your Fragment when the Fragment's 
onDestroy is called. This way your Fragment's instance will survive 
Activity rotations, etc.

- If your Fragment has data/tasks that can be discarded when the screen 
(activity) is no longer visible or accessible to the user:
In the onDetach of your Fragment, destroy/releases/stop the data/task, 
since the user will no longer be interested in the result of the data/task. 
Whether you'd like to implement this or not, depends on your app, on the 
functionality of that screen.

- You don't need to add multiple UI-less fragments to your activity 
(depending on your app's needs). One is enough:
In the onCreate of your activity, user the FragmentManager to see if your 
Fragment is already there (use the findFragmentBy... methods).
If it is there, all is fine.
If it is not there, create a new instance of your UI-less fragment and 
add/commit it to your activity.
This makes sure you'll have only one instance of your Fragment that 
survives orientation changes and such (provided that the 
setRetainInstance(true) was called in the Fragment's onCreate). 


On Friday, February 1, 2013 10:18:29 PM UTC-5, Greenhand wrote:
>
> Thank you for your response. If I use a fragment as a worker thread, I 
> will adapt the pattern in FragmentRetainInstance.java. Because it is 
> unnecessary for a worker fragment to start a asynctask, the worker thread 
> can do the task directly by using java.lang.Thread.
>  
> However, I still do not understand whether it is okay to add multiple 
> invisible fragments as worker threads to the UI and why it tries to stop 
> the thread in both onDestroy() and onDetach().
>
> 2013/2/1 Streets Of Boston <flying...@gmail.com <javascript:>>
>
>> After your Fragment, the one without a UI, has started the AsyncTask, 
>> i.e. after the AsyncTask's 'execute' method has been called, it can stop it 
>> by calling 'cancel' on it.
>>
>>
>> ....
>> ....
>> task.execute(...);
>> ...
>> ...
>>
>>
>> And on some event, (Fragment's onDestroy for example), you just call.
>> task.cancel(true);
>>
>> This will remove the task if it was scheduled or, if it is already 
>> running, interrupt the thread on which it is running.
>> See more info about cancelling 
>> here<https://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)>
>> .
>>
>>  
>>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to