Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up with a solution for this problem without any major workaround: The basic idea how to maintain a progressdialog and a asynctask is described in this <a href="http://blog.doityourselfandroid.com/2010/11/14/handling-progress-dialogs-and-screen-orientation-changes/" rel="nofollow">blogentry</a> (of course I used the AsyncTaskComplex-Version). All credits go to the author of this blogentry, I only added a tiny thing:</p> <p>Obviously I'm not using showDialog() anymore. Instead I stick with DialogFragments.</p> <p>The second tweak is the importent one and also solves the problem with the IllegalStateException:</p> <p>Instead of only telling the asynctask in onRetainCustomNonConfigurationInstance() that there is no more activity I also do it in onPause(). And instead of only telling the asynctask in onCreate() that there is a new activity I also do it in onResume().</p> <p>And there you go, your AsyncTask will not try to inform your activity about his finish causing an IllegalStateException when the activity is not visible.</p> <p>If you would like to see more code instead of words, leave a comment.</p> <p><strong>/edit:</strong> Sourcecode to show my solution, which I think is a pretty decent one :)</p> <pre><code>public class MyActivity extends Activity { private MyTask mTask; @Override protected void onCreate(Bundle pSavedInstanceState) { super.onCreate(pSavedInstanceState); setContentView(R.layout.editaccount); Object retained = getLastCustomNonConfigurationInstance(); if ( retained instanceof NewContactFolderIdTask ) { mTask = (MyTask) retained; mTask.setActivity(this); } } @Override protected void onPause() { if(mTask != null) { mTask.setActivity(null); } super.onPause(); } @Override public Object onRetainCustomNonConfigurationInstance() { if(mTask != null) { mTask.setActivity(null); return mTask; } return null; } @Override protected void onResume() { if(mTask != null) { mTask.setActivity(this); } loadValues(); // or refreshListView or whatever you need to do super.onResume(); } public void onTaskCompleted() { loadValues(); // or refreshListView or whatever you need to do DialogFragment dialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_FRAGMENT); if(dialogFragment != null) { dialogFragment.dismiss(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in Action Bar clicked; go home Intent intent = new Intent(this, OXClient.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; case R.id.menu_refresh: mTask = new MyTask(this); mTask.execute(); break; } return super.onOptionsItemSelected(item); } private class NewContactFolderIdTask extends AsyncTask&lt;Boolean, Integer, Bundle&gt; { private MyActivity mActivity; private boolean mCompleted; private NewContactFolderIdTask(MyActivity pActivity) { this.mActivity = pActivity; } public void setActivity(MyActivity pActivity) { this.mActivity = pActivity; if(mCompleted) { notifiyActivityTaskCompleted(); } } private void notifiyActivityTaskCompleted() { if(mActivity != null) { mActivity.onTaskCompleted(); } } @Override protected Bundle doInBackground(Boolean... pBoolean) { // Do your stuff, return result } @Override protected void onPreExecute() { DialogFragment newFragment = ProgressDialogFragment.newInstance(); newFragment.show(getSupportFragmentManager(), PROGRESS_DIALOG_FRAGMENT); } @Override protected void onPostExecute(Bundle pResult) { mCompleted = true; notifiyActivityTaskCompleted(); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload