Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recently ran into this problem as well. I have an application with many activities all needing to access global objects (db connections, lists of data, etc) and so I overrode the Application class. To that end, I realized all I needed was a reference to the latest instance of the progress dialog in my runnable that I was using to dismiss the dialog. This is what I came up with and it gracefully survives phone reorientations:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); mHandler = new Handler(); mApplication = (MyApplication)getApplication(); mFileStorage = (CheckBoxPreference)getPreferenceScreen().findPreference("fileStorage"); mProgressDialog = new ProgressDialog(this); mProgressDialog.setTitle("Location Update"); mProgressDialog.setMessage("Moving databases to new storage location."); mProgressDialog.setIndeterminate(true); mProgressDialog.setCancelable(false); mProgressDialog.setCanceledOnTouchOutside(false); mApplication.setProgressDialog(mProgressDialog); } @Override protected void onResume() { super.onResume(); mPreferences = PreferenceManager.getDefaultSharedPreferences(this); mPreferences.registerOnSharedPreferenceChangeListener(this); if (sUpdateThread != null &amp;&amp; sUpdateThread.isAlive()) { mProgressDialog.show(); } } @Override protected void onPause() { super.onPause(); mPreferences.unregisterOnSharedPreferenceChangeListener(this); if (sUpdateThread != null &amp;&amp; sUpdateThread.isAlive()) { mProgressDialog.hide(); mApplication.setProgressDialog(null); } } </code></pre> <p>....random stuff here....</p> <pre><code>private Runnable showProgressRunnable = new Runnable() { public void run() { mProgressDialog.show(); } }; private Runnable hideProgressRunnable = new Runnable() { public void run() { if (mApplication.getProgressDialog() != null) { mApplication.getProgressDialog().dismiss(); } } }; </code></pre> <p>My thread does an mHandler.post(showProgressRunnable) when the task starts and then does an mHandler.post(hideProgressRunnable) when the task finishes. Since the reference to the latest ProgressDialog is now stored in the Application class, I can close it out reliably since I am no longer holding on to an old object reference.</p> <p>Link for posterity: <a href="http://developer.android.com/reference/android/app/Application.html" rel="nofollow noreferrer">http://developer.android.com/reference/android/app/Application.html</a></p>
    singulars
    1. This table or related slice is empty.
    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.
    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