Rahul Gupta
Rahul Gupta

Reputation: 5295

AyncTask stops if screen rotates

i have read many questions regarding this only but unable to find a correct solution...The issue is that i have created a dummy AsyncTask and while it is running , i rotate the screen and the AsyncTask stops abruptly.

So i am unable to save its state such that when the screen rotates it resumes from that state only

I can't use this solution as i am not using Fragment :- http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Also, i don't want any solution saying that lock your screen orientation while the Async is running

   public class MainActivity extends Activity{
      //Some Code
      ProgressDialog progressDialog; // Global Variable
      //OnCreateMethod Here
      //.......
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DummyTask dt = new DummyTask();
        dt.execute();
      }
      private class DummyTask extends AsyncTask<Void, Integer, Void> {

    @Override
    protected void onPreExecute() {
      progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Processing...");
            progressDialog.setMessage("Please wait.");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
    }


    @Override
    protected Void doInBackground(Void... ignore) {
      try{
          Thread.sleep(3000);
      }
      catch(Exception e){}
      return null;
    }

    @Override
    protected void onPostExecute(Void ignore) {
      if (progressDialog!=null) {
    pd.dismiss();


     }
        Toast.makeText(getApplicationContext(),"Running",Toast.LENGTH_SHORT).show();
     }
}

Upvotes: 0

Views: 100

Answers (2)

Mike
Mike

Reputation: 1000

Depending on your requirements, using a Service can be a better implementation:

public class DownloaderService extends Service {

    private LongRunningAsyncTask task = null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // Start task
        task = new LongRunningAsyncTask();
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }

}

And you can start your Service from your Activity:

Intent intent = new Intent(this, DownloaderService.class);    
startService(intent);

You can even call startService multiple times and it will always go to the same DownloaderService instance. So make sure your not starting AsyncTask if one is already running. And one way to communicate with any Activity you can use broadcasts.

Also, even if you're not using a Service make sure you understand how different Android versions executes AsyncTask. Please read up on this:

Upvotes: 0

Mike
Mike

Reputation: 1000

There are a few ways to work with AsyncTask and configuration changes. I'd say you basically have two options:

  1. When a configuration change is detected (ie. your Activity goes through the destroy lifecycle events) you cancel your AsyncTask and restarts them when the new Activity gets created
  2. You can detach and attach it using retain configuration

See CommonsWare example of #2 here: https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java

Upvotes: 1

Related Questions