Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>onStop() is the more reliable location to put timer.cancel() (or both places if you want to cover <em>nearly</em> every situation such as circumstances stated at the end <a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" rel="nofollow">here</a>).</p> <p>onPause() is not called in every situation, it is used when onResume() is reasonably likely to happen, such as when you proceed to another activity and then return. Note the indentation in the <a href="http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle" rel="nofollow">lifecycle outline</a>. It may not be called when the back button is pressed. </p> <p>EDIT 1: this is a pre-edit answer, so may not cover the force-close issue.</p> <p>EDIT 2:</p> <pre><code>@Override public void onStop(){ super.onStop(); try { timer.cancel(); } catch (Exception ex) { Log.e("MY APP ERROR", ex.getMessage()); } } </code></pre> <p><em>I</em> have always put the call to super first in these lifecycle methods. I'm not sure if it makes a difference. You'll likely want to set the try/catch statement as well, especially if you choose to include timer.cancel() in both onStop() AND onPause().</p> <p>EDIT 3: About ready to concede defeat. I'm not that familiar with the quirks of ScheduledExecutorService, and since you've stated you're willing to rewrite, here's how I've accomplished the same things in my apps. Handler is an Android-specific implementation, so perhaps thats why I haven't had the same trouble you are having.</p> <pre><code>Handler timer; Runnable runner; @Override onCreate(...) { ... //EDIT 4: (reading time from preferences) SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); final int c = Integer.parseInt(SP.getString("appRefreshRate","20")); ... runner = new Runnable() { @Override public void run() { timer.postDelayed(runner, c * 1000); webview.reload(); } }; timer = new Handler(); timer.postDelayed(runner, c * 1000); ... } @Override public void onPause(){ super.onPause(); try { timer.removeCallbacks(runner); } catch (Exception ex) { Log.e("MY APP ERROR", ex.getMessage()); } } @Override public void onStop(){ super.onStop(); try { timer.removeCallbacks(runner); } catch (Exception ex) { Log.e("MY APP ERROR", ex.getMessage()); } } </code></pre>
 

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