Sobes.tech
Junior — Middle

Kuidas saab Android rakenduses andmeid salvestada ilma Room ja SharedPreferences teekogade kasutamiseta?

sobes.tech AI

Vastus AI-lt

Without using Room and SharedPreferences in Android, data can be stored in the following ways:

  • Files in internal or external storage:

    • In internal storage (getFilesDir()) you can write private application files.
    • In external storage (getExternalFilesDir()) — for files accessible to the user.
  • Directly using SQLite:

    • Use the SQLiteOpenHelper class to create and manage a database without Room.
  • Serialization to a file:

    • Save objects in a file in JSON, XML, or other formats, with subsequent deserialization.

Example of writing a string to an internal file:

String filename = "myfile.txt";
String fileContents = "Hello World";
try (FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.getBytes());
} catch (IOException e) {
    e.printStackTrace();
}

This way, data can be stored without using Room and SharedPreferences, by choosing the appropriate method depending on the task.