I have an ActivityA that has a button. After clicking the button , another ActivityB opens.
onCreate() method of ActivityB , I run AsyncTask , which connect to the server and extract the results and display it on the screen.
Here is the Activity B code:
public class ActivityB extends Activity { AsyncFetch asyncFetch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(null != savedInstanceState) { return; } asyncFetch = new AsyncFetch(); asyncFetch.execute(); } } private class AsyncFetch extends AsyncTask<Object, Void, Object> { @Override protected void onPostExecute(Object o) { } @Override protected Object doInBackground(Object... params) {
When executing AsyncFetch , when I click the button, click ActivityB not immediately. But will be destroyed upon completion of AsyncTask .
How do I implement this to avoid repeated freezing? I tried below:
@Override protected void onDestroy() { asyncFetch.cancel(true); super.onDestroy(); }
But still, he is waiting for AsyncTask cancel or terminate.
Like a GMail app when I open something. Although in the process, if I click the back button, it just exits this Activity .
Is there any other way to implement the background process? Any design pattern to implement this? Please let me know.
Any help is appreciated.
source share