Never mind the name of the async task....its ParentActivityTask.

2011/10/4 João Rossa <[email protected]>

> Actually my async task is not a inner static class, and it still has a
> explicit reference to the activity that i refresh,
>
> *here's the parent activity:*
>
> public abstract class ParentActivity extends Activity{
>
> protected static int progressBarstate=ProgressBar.GONE;
> protected static int themeId;
>
> private ParentActivityTask task=null;
>
> //this method will get the id for the layout
>  public abstract int getLayoutId();
> //this method will launch respejcting oncreate logic for each activity
>  public abstract void initActivity();
> //thi smethod will return window header menu context text string
>  public abstract String getWindowTitle();
> //this method is used to refresh contents for a screen
>  public abstract String updateResultsInUi();
> //nethod for definined specific theme
>  public abstract int getThemeId();
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
>
>
> //set theme first for showing correct header
> setTheme(getThemeId());
>
> super.onCreate(savedInstanceState);
>
> themeId=getThemeId();
>  //in case the screen will have a header
> if(getThemeId()!=R.style.CustomThemeNoHeader){
>
> requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
>
> setContentView(getLayoutId());
>
> getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
> R.layout.custom_window_title);
>
> //save progressview on application context
>  QuadrosMobileApplicationContext.getInstance().setProgressView((LinearLayout)
> findViewById(R.id.progressView));
>
> //set text header
>  ((TextView)findViewById(R.id.header_title)).setText(getWindowTitle());
>
>  progressRefreshState(progressBarstate);
>
> }else
> {
>  setContentView(getLayoutId());
> }
>
> //execute subactivity logic
>  initActivity();
>
> }
>
> /**
>  * check for loading/spinner icon in
>  *  case there's a thread working in background
>  */
> @Override
> public void onRestart () {
>  super.onRestart();
> if(getThemeId()!=R.style.CustomThemeNoHeader){
> QuadrosMobileApplicationContext.getInstance().setProgressView((LinearLayout)
> findViewById(R.id.progressView));
>  progressRefreshState(progressBarstate);
>
> }
> }
>
> @Override
> protected void onResume() {
> super.onResume();
>
>
> QuadrosMobileApplicationContext.getInstance().refreshAsyncTasksActivity(this);
> }
>
>
> @Override
> protected void onPause() {
> super.onPause();
>
> QuadrosMobileApplicationContext.getInstance().clearActivityAsyncTasks();
> }
>
> @Override
> public Object onRetainNonConfigurationInstance() {
>  if(task!=null)
> task.detach();
>
> return(task);
>  }
>
> /**
>  * refresh static references for both progress
>  * info views and set visibility state
>  */
> private void progressRefreshState(int state) {
>  findViewById(R.id.progressView).setVisibility(state);
> }
>
>  /**
>  * do asynctask for background work
>  */
>  public void doAsyncTask(){
> //get task back in case of rotation
> task= (ParentActivityTask)getLastNonConfigurationInstance();
>
> if(task==null){
> task= new ParentActivityTask(this);
> task.execute();
>  //add to the set of tasks
> QuadrosMobileApplicationContext appliContext=
> (QuadrosMobileApplicationContext)getApplicationContext();
>  appliContext.getAsyncTasks().add(task);
> }else{
> task.attach(this);
>  }
> }
>  }
>
> here's the async task as non inner class:
>
> ublic class ActivityTask extends AsyncTask<Void, Void, String>{
>  ParentActivity activity=null;
>
>
> public ActivityTask(QuadrosMobileActivity activity) {
>
> attach(activity);
> }
>
> @Override
>  protected void onPreExecute() {
>
> if(QuadrosMobileActivity.themeId!=R.style.CustomThemeNoHeader){
>
> ParentActivity.findViewById(R.id.progressView).setVisibility(ProgressBar.VISIBLE);
> }
>  QuadrosMobileActivity.progressBarstate=ProgressBar.VISIBLE;
> }
>
> @Override
> protected String doInBackground(Void... params) {
>  return ParentActivity.updateResultsInUi();
> }
>
> @Override
> protected void onPostExecute(String result) {
>  if(activity!=null)
> Toast.makeText(activity, result, Toast.LENGTH_LONG).show();
>  QuadrosMobileApplicationContext appliContext=
> QuadrosMobileApplicationContext.getInstance();
> appliContext.getAsyncTasks().remove(this);
>
> if(appliContext.getAsyncTasks().isEmpty()){
> if(QuadrosMobileActivity.themeId!=R.style.CustomThemeNoHeader){
>
> ParentActivity 
> .findViewById(R.id.progressView).setVisibility(ProgressBar.GONE);
> }
>  QuadrosMobileActivity.progressBarstate=ProgressBar.GONE;
> }
> }
>
> void detach() {
> activity=null;
> }
>
> void attach(ParentActivity activity) {
> this.activity=activity;
>  }
> }
>
>
> On Tue, Oct 4, 2011 at 12:18 AM, TreKing <[email protected]> wrote:
>
>> On Mon, Oct 3, 2011 at 5:28 PM, Studio LFP <[email protected]> wrote:
>>
>>> When this happens, you need to save off the position in your AsyncTask
>>> and restart it back when the activity gets restarted.
>>
>>
>> The OP is using getLastNonConfigurationInstance(), which would indicate
>> he's keeping the same AsyncTask instance across the application restart, so
>> he wouldn't have to restart the AsyncTask.
>>
>> 2011/10/3 João Rossa <[email protected]>
>>
>>> Well when i rotate, either while the thread is started or after the
>>> screen data is refreshed it goes back to the fields having not been filled.
>>> but the thread does run because if i dont rotate the UI views are filled
>>> with the content i fetch from the server.
>>
>>
>> Looks like you AsyncTask is an inner-class of your Activity, which means
>> that when you start it, it has an implicit reference to the Activity that
>> started it. When you rotate the device, that Activity is destroyed but your
>> AsyncTask is still holding on to it, so when you do findViewById() in your
>> Runnable, you're acting on the old instance that is no longer visible.
>>
>> One thing you can do is make the AsyncTask *not* an inner-class and give
>> it an *explicit* reference to the Activity it's working on, then reset that
>> instance to the new Activity once your activity is restarted.
>>
>> Or something like that.
>>
>>
>> -------------------------------------------------------------------------------------------------
>> TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
>> transit tracking app for Android-powered devices
>>
>>  --
>> 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 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

Reply via email to