Persistance

Shared Preferences

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        [FILE_NAME], Context.MODE_PRIVATE);

Shared Preferences

SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("saved_score", newHighScore);
editor.commit();
SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 12;
long highScore = sp.getInt("saved_score", defaultValue);

Fichiers

Fichiers

String fileName = Context.getCacheDir() + FILENAME;
FileOutputStream fos = ctx.openFileOutput(fileName, Context.MODE_PRIVATE);

SQLite

SQLite

public class MySQLiteDB extends SQLiteOpenHelper {
        private static final String TABLE_BOOKS = "table_books";
        private static final String COL_ID = "ID";
        private static final String COL_ISBN = "ISBN";
        private static final String COL_TITLE = "Title";
        private static final String CREATE_BDD = "CREATE TABLE " + TABLE_BOOKS + " ("
        + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ISBN + " TEXT NOT NULL, "
        + COL_TITLE + " TEXT NOT NULL);";
        public MaBaseSQLite(Context context, String name, CursorFactory factory, int version) {
                super(context, name, factory, version);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
                db.execSQL(CREATE_BDD);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // Do your upgrade here
        }
}

SQLite

MySQLiteDB db = new MySQLiteDB(context, NOM_BDD, null, VERSION_BDD);
SQLiteDatabase database = db.getWritableDatabase();
Cursor c = database.query(TABLE_BOOKS, new String[] {
        COL_ID, COL_ISBN, COL_TITLE
}, COL_TITLE + " LIKE \"" + title +"\"", null, null, null, null);
ContentValues values = new ContentValues();
values.put(COL_ISBN, book.getIsbn());
values.put(COL_TITLE, book.getTitre());
return bdd.update(TABLE_BOOKS, values, COL_ID + " = " +id, null);