Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I actually had this issue and since my application wants to store a more complex object I did this I took the object serialized it then stored it to a file. I can then check from any activity this file and get the full object. No need to use the string key garbage of prefs and Bundles.</p> <pre><code>/** * @param act - current activity calling the function * @return false if global Object not set and the dat file is not there or empty */ public static void setInFile(Activity act, Object_VO obj) { ((Object) act.getApplication()).setObjectState(obj); String ser = SerializeObject.objectToString(obj); if (ser!= null &amp;&amp; !ser.equalsIgnoreCase("")) { SerializeObject.WriteSettings(act, ser, "android.dat"); } else { SerializeObject.WriteSettings(act, "", "android.dat"); } } </code></pre> <p>You will need this to serialize and deserialize:</p> <pre><code>/** * Take an object and serialize and then save it to preferences * */ public class SerializeObject { /** * Create a String from the Object using Base64 encoding * @param object - any Object that is Serializable * @return - Base64 encoded string. */ public static String objectToString(Serializable object) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { new ObjectOutputStream(out).writeObject(object); byte[] data = out.toByteArray(); out.close(); out = new ByteArrayOutputStream(); Base64OutputStream b64 = new Base64OutputStream(out,0); b64.write(data); b64.close(); out.close(); return new String(out.toByteArray()); } catch (IOException e) { e.printStackTrace(); } return null; } /** * Creates a generic object that needs to be cast to its proper object * from a Base64 ecoded string. * * @param encodedObject * @return */ public static Object stringToObject(String encodedObject) { try { return new ObjectInputStream(new Base64InputStream( new ByteArrayInputStream(encodedObject.getBytes()), 0)).readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * Save serialized settings to a file * @param context * @param data */ public static void WriteSettings(Context context, String data, String filename){ FileOutputStream fOut = null; OutputStreamWriter osw = null; try{ fOut = context.openFileOutput(filename, Context.MODE_PRIVATE); osw = new OutputStreamWriter(fOut); osw.write(data); osw.flush(); //Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); // Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); } finally { try { if(osw!=null) osw.close(); if (fOut != null) fOut.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Read data from file and put it into a string * @param context * @param filename - fully qualified string name * @return */ public static String ReadSettings(Context context, String filename){ StringBuffer dataBuffer = new StringBuffer(); try{ // open the file for reading InputStream instream = context.openFileInput(filename); // if file the available for reading if (instream != null) { // prepare the file for reading InputStreamReader inputreader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputreader); String newLine; // read every line of the file into the line-variable, on line at the time while (( newLine = buffreader.readLine()) != null) { // do something with the settings from the file dataBuffer.append(newLine); } // close the file again instream.close(); } } catch (java.io.FileNotFoundException f) { // do something if the myfilename.txt does not exits Log.e("FileNot Found in ReadSettings", "filename = " + filename); } catch (IOException e) { Log.e("IO Error in ReadSettings", "filename = " + filename); } return dataBuffer.toString(); } } </code></pre> <p>and finally you need to create global object state you can reference.</p> <pre><code>package com.utils; /** * Global class to hold the application state of the object * */ public class ObjectState extends Application { private Object_VO obj; public Object_VO getObjectState() { return this.obj; } public void setObjectState(Object_VO o) { this.obj = o; } } </code></pre> <p>Oh yeah and you need to update your AndroidManifest.xml file to include the reference to the ObjectState since it references Application like this...</p> <pre><code> &lt;application android:name="com.utils.ObjectState" android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".MainActivity" android:label="@string/app_name"&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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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