Saving Parcelable Data
Solution 1:
Using static in your example leads to memory leaks and is not a good way to do anything.
I suggest using static only in 3 cases:
- static final String or int - constants
- on inner classes (so that they don't contain reference to outer class)
- on util or in some cases (like CustomFragment.newInstance) factory methods
The question is why would you want to persist PendingIntent? Its usecase is for inter-process-communication.
Solution 2:
I use a StateControl class to handle reading/writing to disc:
publicclassStateControl {
Context mContext;
Thread worker;
WriteObjectToFile writer;
// StateControl Constructor publicStateControl(Context context) {
mContext = context;
// Construct a writer to hold and save the data
writer = newWriteObjectToFile();
// Construct a worker thread to handle the writer
worker = newThread(writer);
}// end of StateControl constructor// Method to save the global datapublicvoidsaveObjectData(Objectobject, String key) {
if (object == null){
// I had a different action here
} else {
// Write the data to disc
writer.setParams(newWriteParams(object, key));
worker.run();
}
}// end of saveGlobalData method// Method to read the Global DatapublicObjectreadObjectData(String key){
Object returnData = (Object) readObjectFromFile(key);
if (returnData == null){
// I had a different action here
} else {
return returnData;
}
}// end of readGlobalData method// Method to erase the Global datapublicvoidclearObjectData(String key){
writer.setParams(newWriteParams(null, key));
worker.run();
}// end of clearGlobalData method privateclassWriteObjectToFileimplementsRunnable {
WriteParams params;
publicvoidsetParams(WriteParams params) {
this.params = params;
}
publicvoidrun() {
writeObjectToFile(params.getObject(), params.getFilename());
}
privatebooleanwriteObjectToFile(Objectobject, String filename) {
boolean success = true;
ObjectOutputStream objectOut = null;
try {
FileOutputStream fileOut = mContext.openFileOutput(filename, Activity.MODE_PRIVATE);
objectOut = newObjectOutputStream(fileOut);
objectOut.writeObject(object);
fileOut.getFD().sync();
} catch (IOException e) {
success = false;
e.printStackTrace();
} finally {
if (objectOut != null) {
try {
objectOut.close();
} catch (IOException e) {
// do nothing
}
}// end of if
}// End of try/catch/finally blockreturn success;
}
}// end of writeObjectToFile methodprivateObjectreadObjectFromFile(String filename) {
ObjectInputStream objectIn = null;
Objectobject = null;
try {
FileInputStream fileIn = mContext.getApplicationContext().openFileInput(filename);
objectIn = newObjectInputStream(fileIn);
object = objectIn.readObject();
} catch (FileNotFoundException e) {
// Do nothing
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (objectIn != null) {
try {
objectIn.close();
} catch (IOException e) {
// do nowt
}
}
}
returnobject;
}
privatestaticclassWriteParams {
Objectobject;
String filename;
publicWriteParams(Objectobject, String filename) {
super();
this.object = object;
this.filename = filename;
}
publicObjectgetObject() {
returnobject;
}
publicStringgetFilename() {
return filename;
}
}
}
Then invoke the public methods to kick off the writing/reading. For this version, I also having it taking place in a separate thread, but you could modify that if you needed to.
Solution 3:
Binder
Most developers will not implement this class directly, instead using the aidl tool to describe the desired interface, having it generate the appropriate Binder subclass.
from the official documentation
Do you need to store the Binder
object with the rest of your object? Maybe you can save your object without the Binder
instance, and re-create the Binder
object with aidl
after you restore the object
Post a Comment for "Saving Parcelable Data"