iink SDK on Device

Timing for saving files

I am having problems with saving and reloading files with the iink SDK.

I started with the sample project "GetStarted" and replaced the code to create a file in onCreate() with the following code:

---------------------------------

 if (file.exists()) {
      try {
        contentPackage = engine.openPackage(file);
      } catch (IOException e) {
        Log.e(TAG, "Failed to open package \"" + packageName + "\"", e);
        e.printStackTrace();
        return;
      }
      contentPart = contentPackage.getPart(0);
    } else {
      try {
        contentPackage = engine.createPackage(file);
      } catch (IOException e) {
        Log.e(TAG, "Failed to create package \"" + packageName + "\"", e);
        e.printStackTrace();
        return;
      }
      contentPart = contentPackage.createPart("Text Document");
    }

 --------------------------------------

Basically, I check if the file already exists and try to open it if it does.

The I added saving code to onDestroy as below:

-------------------------------

if (contentPackage != null)
    {
      try {
        contentPackage.save();
      } catch (IOException e) {
        Log.e(TAG, "Failed to save package!", e);
        e.printStackTrace();
      }
      contentPackage.close();
      contentPackage = null;
    }

 ------------------------------

Finally, I overrode onSaveInstanceState to save to temp

-------------------------------- 

@Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    if (contentPackage != null) {
      try {
        contentPackage.saveToTemp();
      } catch (IOException e) {
        Log.e(TAG, "Failed to save to temp!", e);
        e.printStackTrace();
      }
    }
  }

---------------------------

With the above, if I send the app to the background and then kill it with the Android task selection button, then in onCreate file.exists() returns false but engine.createPackage(file) returns an exception saying the package file already exists.

My guess is that the package file becomes corrupted from saving to temp and never closing the file because onDestroy is never called in the above scenario.

I also looked at GetStarted which doesn't seem to run into this problem, but its handling of the file is much more complex and I could not find where it differs in when each operation is performed.

So I would like to know on which events it is recommended to open, close and save files.

Thank you!

1 Comment

Dear Nicolas,


currently, you will learn a lot about managing packages in the following link: https://developer.myscript.com/docs/interactive-ink/1.4/android/fundamentals/storage/#working-with-packages


 Without knowing exactly how you implemented your application, it is difficult to provide with an accurate answer. Indeed, open or create package can be done at startup, or on demand... depending on your use-case. And saving and closing can also be done when application is closed, or on-demand... our recommendation would be that by default, you proceed as in our getStarted, i.e. in the onDestroy, you wait the editor is in idle, set editor to null, close the contentPart and contentPackage.


Best regards,


Olivier


1 person likes this