title: Function not saving data to file onDestroy() Android
body: <p>I do the following function in my main activity</p>
  
  <pre><code> @Override
   public void onDestroy() {
   super.onDestroy();
   SimpleBookManager.getSimpleBookManager().saveChanges(this);  
  }
  </code></pre>
  
  <p>When Android later call the method function saveChanges() when I close the Application I get the "SaveChanges" outprint but not the last print "After Saving" meaning that its not executing the whole function and I'm not getting any outPrints on the catch exceptions lines either. Do you know why? I'm new to android and do not understand if you could run this code onDestroy(). =)</p>
  
  <pre><code>import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.util.ArrayList;
  
  import android.content.Context;
  
  public class SimpleBookManager implements BookManager, java.io.Serializable {
  
      private ArrayList&lt;Book&gt; allBooks = new ArrayList&lt;Book&gt;();
      private static SimpleBookManager ref;       
  
  
      public void saveChanges(Context context){
          try{
              System.out.println("SaveChanges");
              FileOutputStream f_out = context.openFileOutput("myobject.data",Context.MODE_PRIVATE);
  
              // Write object with ObjectOutputStream
              ObjectOutputStream obj_out = new    ObjectOutputStream (f_out);
          // Write object out to disk
          obj_out.writeObject(allBooks);
          System.out.println("After Saving");
  
      }catch (FileNotFoundException e) {
          System.out.println("No file found saveChanges");
      } catch (IOException e) {
          System.out.println("IOException in SaveChanges");
      }   
  }
  
  
  
  
  public void loadBooks(Context context){
      try{
      // Read from disk using FileInputStream
          FileInputStream f_in = context.openFileInput("myobject.data");
  
      // Read object using ObjectInputStream
      ObjectInputStream obj_in =  new ObjectInputStream (f_in);
  
      // Read an object
      Object obj = obj_in.readObject();
      //System.out.println(obj);
      if (obj instanceof ArrayList&lt;?&gt;)
      {
          allBooks = (ArrayList&lt;Book&gt;) obj;
  
          System.out.println("Worked");
          System.out.println(allBooks);
  
      }
      }catch (FileNotFoundException e) {
          System.out.println("No file found LoadBooks");
      } catch (IOException e) {
  
  
      System.out.println("IOException in loadBooks");
      } 
      catch (ClassNotFoundException e) {
          System.out.println("ClassNotFound in loadBooks");
      }
  
  }
  
    public static SimpleBookManager getSimpleBookManager()
    {
      if (ref == null){
          ref = new SimpleBookManager();
      }
  
      return ref;
    }
  public SimpleBookManager(){
      Book book1 = createBook();
      book1.setAuthor("RandomBookAuthor");
      book1.setCourse("Kunskap");
      book1.setPrice(199);
      book1.setTitle("Some Random Title");
      book1.setIsbn("9780590353403");
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>I also get IOException when doing this onCreate() in main, so something is really fishy about my load and save functions.</p>
  
  <pre><code>    @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          SimpleBookManager.getSimpleBookManager().loadBooks(this);
          setContentView(R.layout.activity_action_bar_main);
  }
  </code></pre>
  
  <p><strong><em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em></strong></p>
  
  <p>I have added the permission to the manifest to write to storage in the last lines of the manifest below.</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.av.BookApp"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="11"
          android:targetSdkVersion="16" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
  
          &gt;
          &lt;activity
              android:name=".BookApp"
              android:label="@string/title_bookapp"
              android:uiOptions="splitActionBarWhenNarrow" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
              &lt;activity
          android:name=".AddBookActivity"
          android:uiOptions="splitActionBarWhenNarrow" &gt;
          &lt;meta-data
              android:name="android.support.PARENT_ACTIVITY"
              android:value="edu.chalmers.BookApp" /&gt;
      &lt;/activity&gt;
       &lt;activity
          android:name=".DetailActivity"
          android:uiOptions="splitActionBarWhenNarrow" &gt;
          &lt;meta-data
              android:name="android.support.PARENT_ACTIVITY"
              android:value="edu.chalmers.BookApp" /&gt;
      &lt;/activity&gt;
       &lt;activity
          android:name=".EditBookActivity"
          android:uiOptions="splitActionBarWhenNarrow" &gt;
          &lt;meta-data
              android:name="android.support.PARENT_ACTIVITY"
              android:value="edu.chalmers.BookApp" /&gt;
      &lt;/activity&gt;
  
      &lt;/application&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>After I followed the advice below I now get a IOException in saveChanges()</p>
  
  <p>And I do set the data correct like this (In SimpleBookManager)</p>
  
  <pre><code>public SimpleBookManager(){
  Book book1 = createBook();
      book1.setAuthor("RandomBookAuthor");
      book1.setCourse("Kunskap");
      book1.setPrice(199);
      book1.setTitle("Some Random Title");
      book1.setIsbn("9780590353403");
  }
  </code></pre>