What is the right place to store persistent app data before exiting?
<p>I have a single <code>Activity</code> application with a data container that is populated at the launch of the application from a JSON formatted private text file. When exiting the application, whether it's a graceful exit or forced one, the the data must be written back to the text file.</p>

<p>Android documentation <a href="http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29" rel="nofollow">states</a> that application data shouldn't be saved in the onDestroy method.</p>

<p>I have now overwritten the following methods to make sure the data is stored at exit:</p>

<pre><code>protected void onStop() {
    persistenceManager.storeIfChanged();
    super.onStop();
}

protected void onPause() {
    persistenceManager.storeIfChanged();
    super.onPause();
}
</code></pre>

<p>The <code>storeIfChanged</code> methods writes the application data to the text file if it has changed since last save. <strong>My question is</strong>, does this guarantee the application data is saved no matter how the application is terminated (unless it's force majeure). <strong>Secondly</strong>, is it superfluous to do the saving in both onStop and onPause?</p>
