Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the documentation on <a href="http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState" rel="nofollow">Saving Persistent State</a>,</p> <blockquote> <p>There are generally two kinds of persistent state than an activity will deal with: shared document-like data (typically stored in a SQLite database using a content provider) and internal state such as user preferences.</p> <p>For content provider data, we suggest that activities use a "edit in place" user model. That is, any edits a user makes are effectively made immediately without requiring an additional confirmation step. Supporting this model is generally a simple matter of following two rules:</p> <p>When creating a new document, the backing database entry or file for it is created immediately. For example, if the user chooses to write a new e-mail, a new entry for that e-mail is created as soon as they start entering data, so that if they go to any other activity after that point this e-mail will now appear in the list of drafts. When an activity's onPause() method is called, it should commit to the backing content provider or file any changes the user has made. This ensures that those changes will be seen by any other activity that is about to run. You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.</p> </blockquote> <p>So if you want to do it "correctly", I would save the data in <code>onPause</code>... and I'd probably save the state using an SQLite database of some sorts. You should also perform file I/O on a separate thread using an <code>AsyncTask</code>, as this sort of thing could potentially block the UI thread and crash your app.</p> <p>If you want a quick and dirty way to do it (<strong>i.e. if you are not releasing this application on the Android market</strong>), then I am betting that the problem is that you are trying to perform the file I/O in <code>onDestroy</code>, which is not guaranteed to be called. This is another reason to perform the file reads/writes in <code>onPause</code>.</p> <p>The last thing I would suggest is reading through the documentation on <a href="http://developer.android.com/guide/topics/data/data-storage.html#filesInternal" rel="nofollow">internal/external storage</a>. It could be that you aren't writing to the correct directory because you don't have the file permissions to do so. You should perform the file I/O like so:</p> <pre><code>String FILENAME = "FLIGHTS"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(...); fos.close(); </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