Otto
Otto

Reputation: 19

Saving the state of an activity to a name/or file

so i am creating a save system for my aplication, where each item in my recycler view in my (Mainactivity1), opens the same activity(MainActivity2). but i then want some way so that when i click on a button in (Mainactivity2) it saves the state of the activity(MainActivity2), but to a new name, so that i can load the activity from(MainActivity1). How is that done? in theory? I just want to know how to save the instance of an activity to a name, loading the activity will be my headache.

Thank you in advance.

Upvotes: 0

Views: 42

Answers (1)

Tarun
Tarun

Reputation: 986

If you are trying to save an object. Then Java Serialization could help you

To Write Object

File fileToSaveObject=new File("path");
Object objectToSave=new Object();

FileOutputStream fileOut = new FileOutputStream(fileToSaveObject);
ObjectOutputStream out = new ObjectOutputStream(fileOut);

out.writeObject(objectToSave); // It will save 'objectToSave' in given file

out.close();
fileOut.close();  

To Read Object

File fileToReadObject=new File("path");
Object objectToRead;

FileInputStream fileIn = new FileInputStream(fileToReadObject);
ObjectInputStream in = new ObjectInputStream(fileIn);

objectToRead= (Object) in.readObject();  // It will return you the saved object

in.close();
fileIn.close();

Upvotes: 1

Related Questions