0

I had a simple program where i need to update the list and text based on the server response ... But Asynctask onpostexecute is not updating views if the screen is rotated while doinbackground is executed .

I came to know the reason that , as the activity is recreated , onpostexecute wont update its views (Same problem..here is the link : Chek this link) But i was not satisfied with the answer as it just suggesting to restricting to recreate the activity (i want recreating the activity as in my project i had some extra layout in landscape mode).

Please dont suggest setretaininstance(true) by taking fragments as it doesnt call oncreateview(), which is not suitable for my project.

May be as lastoption i can restrict orientation programatically in onpreexecute and release it in onpostexecute. But still it will not be good practice i think.

public class MainActivity extends ActionBarActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView) findViewById(R.id.textview);
        if(savedInstanceState==null)
        {
            new myAsync().execute();
        }
    }


    public class myAsync extends AsyncTask<Void, String, Void>
    {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            textView.setText("started");
            Log.e("started", "started");
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            Log.e("executed", "executed");
        }

    }
}

This is my sample program . textview is not updating if screen is rotated.

Please suggest . Thanks in advance .

Community
  • 1
  • 1
Avinash
  • 123
  • 1
  • 12
  • 1
    Use `AsyncTaskLoader` instead of `AsyncTask`. It handles the lifecycle and has a cancel/stop functionality provided – ashkhn Jul 19 '15 at 09:05
  • Started working on it..but docs says it is supported from api 11. my app should support from api 9 – Avinash Jul 19 '15 at 11:07
  • Its there in the support library .. [Reference](https://developer.android.com/reference/android/support/v4/content/AsyncTaskLoader.html) – ashkhn Jul 19 '15 at 16:08

2 Answers2

0

You could provide myAsyncTask with a TextView member with a setter and store the current task as static member of the activity.

class MyActivity extends Activity {
  private static AsyncTask myTask = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView = (TextView) findViewById(R.id.textview);

    if (myTask == null) {
      new myAsync(textView).execute();
    } else if(myTask.getStatus() != AsyncTask.Status.FINISHED) {
      myTask.set(textView);
    }
  }

  private class myAsync extends AsyncTask<Void, String, Void>
  {
     TextView textView;

     myAsync(TextView textView) {
       this.textView = textView;
     }

     synchronized void setTextView(TextView textView) {
       this.textView = textView;
     }

     ...

  }
}

You would still have to deal with race conditions. E.g. you would probably want to impelemnt a mechanism to pause/resume your task, whenever the activity pauses/resumes. You'd also have to make sure that the tasks textView is still valid and that you cleanup your static task in onPostExecute.

Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37
0

You can use the concept of bundle to put some string in it. When the activity is recreated after rotation check if saved instance state is null or not in the oncreate method. If not null retrieve the string using the bundle and update the textview. For more information on this rotation thing check out the videos of slidenerd on YouTube in the asynctask and threads playlist. Hope it helps.

mik dass
  • 381
  • 3
  • 16
  • As my async is not still completed . i can not save that in onsavedinstance..once async is completed , then i can have i can have result in onsavedinstance as u said ... – Avinash Jul 19 '15 at 11:06
  • 1
    Try the slidenerd uploader.In the threads and async task playlist ,in video no .17 to 21, he has explained this rotation thing in detail using a image downloading app. Try it out , I think it will really help you. – mik dass Jul 19 '15 at 16:08