Issue
How do I handle references between Fragments showing list of data and different threads/classes updating data in the background. Because of this I need to be able to force update from outside of the Fragments via Observer-inf or something similar.
Currently I'm using dummy factory-class to create a new SqlLiteDatabase object, based on my default settings for it.
public static SQLiteDatabase initFor(Context context) {
db = context.openOrCreateDatabase(NAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setLocale(Locale.getDefault());
db.setVersion(1);
db.setLockingEnabled(true);
return db; }
Then I call this from each fragment and sets up adapters, the same applies to the data-inserter-threads. To close the db, I override my activity's onDestroy() and call: initFor(this).close(); .
This is very ugly and it fails on orientation change. I'm thinking of SQLiteOpenHelper but I don't know how it will help me.
Solution
You should access your database on another thread (in a Service) then have this talk to one class (which could be your helper), you then access this helper class through a singleton initialized in a class that extends Application. You can get your Application class from any of your fragments by calling
((YourApplication) context.getApplicationContext()).getGetDatabaseHelper();
Answered By - Blundell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.