Page Index Toggle Pages: 1 [2]  Send TopicPrint
Hot Topic (More than 10 Replies) Save and load from (Read 14115 times)
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Save and load from
Reply #15 - Oct 9th, 2015 at 5:12pm
Print Post  
Using the Save command and then rotating the tablet to recreate the activity and reload does not throw exception for me. Should I follow any specific steps to see it?
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: Save and load from
Reply #16 - Oct 9th, 2015 at 5:18pm
Print Post  
No, the problem is when you save then close the application and then open the application and then load the saved file, if i comment out this part:

if (savedInstanceState != null && savedInstanceState.containsKey("fileName")) {
Log.d("SAVEDINSTANCESTATE", "I'm on saveInstanceState");
fileName = savedInstanceState.getString("fileName");
//TODO load del file
try {
FileInputStream inputStream = openFileInput(fileName);
//inputStream.reset();

diagram.loadFrom(inputStream);
inputStream.close();
//inputStream.reset();

//diagram.loadFrom(fileName, getApplicationContext());
} catch (IOException e) {
e.printStackTrace();
}


}else if(savedInstanceState != null && savedInstanceState.containsKey("ErrorList")){
Log.d("SAVEDINSTANCESTATE", "I'm on saveInstanceState");

}else {

File file = activity.getApplicationContext().getFilesDir();
String[] arrayFiles = file.list();
for (String f : arrayFiles) {
Log.d("Load file", "File name: " + f);
if (f.compareTo("temp") == 0) {
try {
FileInputStream inputStream = openFileInput("temp");
//inputStream.reset();
diagram.loadFrom(inputStream);
//inputStream.reset();
inputStream.close();
//diagram.loadFrom(f, getApplicationContext());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


and this one:

Override
public void onSaveInstanceState(Bundle outState) {
if(fileName != null && fileName.compareTo("") != 0){
Log.d("onSaveInstanceState", "STO SALVANDO");
outState.putString("fileName", fileName);
}else{
outState.putString("fileName", "temp");
}

outState.putString("ErrorList", "true");
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onDestroy() {

if(fileName != null && (!fileName.equals(""))){
try {
FileOutputStream fileOut = openFileOutput(fileName, Context.MODE_PRIVATE);
fileOut.flush();
diagram.saveTo(fileOut);
fileOut.close();

} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fileOut = openFileOutput("temp", Context.MODE_PRIVATE);
fileOut.flush();
diagram.saveTo(fileOut);
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}

super.onDestroy();

}

it works but the problem is that I need them
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Save and load from
Reply #17 - Oct 9th, 2015 at 5:52pm
Print Post  
I still cannot reproduce. Notice this part of onDestroy help reference:

Quote:
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.


Try moving the onDestroy code to onSaveInstanceState instead to see if it will improve anything.

Also check if you aren't getting any exceptions when saving, e.g. openFileOutput followed by flush() will probably create an empty file on the file system overwriting previously saved one, and then any exception would leave the empty file there, resulting in later load exceptions too.
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: Save and load from
Reply #18 - Oct 9th, 2015 at 6:16pm
Print Post  
Ok, thank you very much, I'll try Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send TopicPrint