I am trying to write an Android game, and I would like it to pause the game, even if the user wants to return to the main menu or the activity will be destroyed by the system. onSaveInstanceState doesn't seem to give me much control over when I can read the package back, plus from what I can tell, the package is only good for short periods of time. So I want to serialize a few ArrayLists that I have, and then read them back. I have no compilation errors and program crashes. But the data is either never written or never read. I'm not sure which one. My serializeData method is being called on onDestroy, and deserializeData is being called from onCreate. Here is my code for writing and reading data:
public void serializeData(String filename, ArrayList<String>arrayList) { FileOutputStream fos; try { fos = openFileOutput(filename, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(arrayList); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } @SuppressWarnings("unchecked") private void deserializeData(String filename, ArrayList<String>arrayList){ try{ FileInputStream fis = openFileInput(filename); ObjectInputStream ois = new ObjectInputStream(fis); arrayList = (ArrayList<String>)ois.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); } }
Any help would be greatly appreciated! Thanks in advance!
source share