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)> . On Wednesday, January 30, 2013 11:30:56 AM UTC-5, Greenhand wrote: > > For asynctask to deal with activity rotation, I find this solution: > https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java > . > However, getlastnonconfigurationinstance() is deprecated in API level 13 ( > http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance() > ) > and fragment is recommended. > I think that fragment is more graceful than asynctask like the code > mentioned above in terms of attaching to and detaching from an > activity because fragment lifecycles manage them. Besides, fragment can be > retained on rotation. > > Therefore, I take a look at * > http://developer.android.com/guide/components/fragments.html*<http://developer.android.com/guide/components/fragments.html> > and > read "For an example activity that uses a fragment as a background worker, > without a UI, see the FragmentRetainInstance.java sample." . > After reading the source code of FragmentRetainInstance.java, I have > question on this line > "getFragmentManager().beginTransaction().add(android.R.id.content, new > UiFragment()).commit();". > It adds the UiFragment()) to the layout and starts the worker thread in > RetainedFragment consequently. > > I would like to modify the worker thread to perform network operation > according to user interaction. For example, when a user fire a > View.OnClickListener event, I add the fragment in order to perform network > operation. > Is it okay to add the fragment multiple times if users fire more than one > event? Will it lead to some side effect (to view hierarchy)? > > The other question, why > synchronized (mThread) { > mReady = false; > mThread.notify(); > } > appears in both onDestroy() and onDetach()? > Aren't they duplicate because the thread is gone when onDestroy() returns? > Why try to stop the thead again in onDetach()? > > -- -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] 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 [email protected]. For more options, visit https://groups.google.com/groups/opt_out.

