Tuesday, February 5, 2013

Re: [android-developers] Re: asynctask vs. FragmentRetainInstance.java in API demo

Instead of Threads or AsyncTasks that do the background work getting the data from the server, use Loader (AsyncLoader).
You can use the LoaderManager to initially load them and also to restart them. Restarting them would allow you to implement your 'refresh' functionality.


On Tuesday, February 5, 2013 10:19:52 AM UTC-5, Greenhand wrote:
As for ".. it is unnecessary for a worker fragment to start a asynctask ...", let me put it in another way.
Yes, I agree a fragment without UI is not a worker thread. Why I said ".. it is unnecessary for a worker fragment to start a asynctask ..." was about the FragmentRetainInstance.java. It does not start a asynctask; It creates a new thread instead. I hope it is clearer.

Thank you for giving clear and detailed explanation about how to use a fragment without a UI. I really appreciate it!
 
With respect to the scenario of adding multiple UI-less fragments to my activity, I would like to use UI-less fragments to perform an HTTP request, such as performing GET request to a RESTful web service. In order to provide the refresh function to users, I need to perform the request again when a corresponding event happens, such as a button click event. I could not figure out another way but only adding the same fragment to my activity. It is why I am concerned about adding multiple UI-less fragements to my activity. Is there another way to do so?
 
2013/2/5 Streets Of Boston <flying...@gmail.com>
".. 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). 
 

--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment