java - Android: save object onPause() and read object onResume() -


i not able save object(custom) file.

the process of app saving object onpause() , getting object onresume().

1) have 2 activities (activity , activity b) 2) saving , reading object in activity  3) activity call activity b on button click. 

here steps replicate problem: (activity launches reading object file.)

1) click on button on activity a. 2) save custom object file in onpause method of activity a. 3) activity b launches 4) click on action bar button. 5) read object file saved in onresume() method of activity a. 6) again click on button on activity a. 7) saves custom object file in onpause() method of activity a. 8) activity b launches. 9) click on button of device. 10) tries read object file in onresume() method of activity a. 

please note: file saving , reading @ same location.

here @ step 10, object read incorrect. suspect @ step 7, object not saved properly.

can saving object file , reading file object?

correct me if wrong. per android document, onsaveinstancestate() , onrestoreinstancestate() not suitable method save.

here manifest:

    <activity         android:name=".activitya"         android:configchanges="orientation|keyboardhidden|screensize"         android:label="@string/app_name"         >     </activity>     <activity         android:name=".activityb"         android:configchanges="orientation|keyboardhidden|screensize"         android:label="@string/app_name" >     </activity> 

saving , reading object here (activitya):

customer customer; @override protected void onpause() {     super.onpause();     save(customer); }  @override protected void onresume() {     super.onresume();     customer = read(); }    private void save(customer customer) {     try {         fileoutputstream fileoutputstream = openfileoutput("customer.properties", context.mode_private);         properties properties = new properties();         properties.put("customer_name", customer.getname());         properties.put("customer_id", customer.getid());         properties.put("customer_place", customer.getplace());      } catch (filenotfoundexception e) {         e.printstacktrace();     } }       private customer read() {      try {           fileinputstream fileinputstream = openfileinput("customer.properties");           properties properties = new properties();           properties.load(fileinputstream);           customer customer = new customer();           customer.setname(properties.get("customer_name"));           customer.setid(properties.get("customer_id"));           customer.setplace(properties.get("customer_place"));           return customer;      } catch(exception e) {      }    return null;   } 

on @override super.onpause() , super.onresume() comes first respectively, can place code.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -