7

I have 2 activities.

ActivityA accesses database through Content Provider, and it start ActivityB.

ActivityB accesses database directly.

I found after ActivityB updated the database, ActivityA querying database by CP and the result won't update.

But the database is actually updated!

How to sync the two methods?

PS: ActivityA and ActivityB are in different Applications.

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
timyau
  • 812
  • 1
  • 16
  • 27
  • Are you really want database sharing? I have code for shared preference sharing.. this enough for you? – Ranjithkumar Jan 06 '17 at 04:02
  • 1
    Post your database operations code. – Pravin Divraniya Jan 06 '17 at 06:45
  • 2
    Where's your code your missing a call to notifyChange() – danny117 Jan 06 '17 at 23:39
  • ActivityA is a launcher, and it start ActivityB. I have called notifyChange() but no updated when ActivityA read the database again. If both activities operating database by Content Provider, the result will be OK. And if activityA or activityB is force stopped from system -> applications, the result also be ok. – timyau Jan 08 '17 at 12:46
  • 1
    Why you dont want export content provider and use them from both applications? – Anton Pogonets Jan 12 '17 at 21:11
  • Originally the database is only for ActivityB, after times the ActivityA is coming and need to access the database. Of course I can export Content Provider for both. I just want to know why.. – timyau Jan 13 '17 at 01:21

3 Answers3

2

Content providers when you update as a courtesy call NotifyChange This is to notify subscribers of your content provider that content has changed.

Activity A needs to access the content provider by URI and register a content observer. Activity B needs to call NotifyChange with a row based URI so that observers of the change will receive a row based URI. Everything depends on the URI used to access the contentprovider. This gives realtime changes b-A. B changes >>> A receives the changes and can do something with them.

Notification/Event for row and column level observation in Content Provider

Reload or notify Fragment's data in ViewPager

Content Provider Update

Community
  • 1
  • 1
danny117
  • 5,581
  • 1
  • 26
  • 35
2

** database **

  @Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub


    db.execSQL("CREATE TABLE " + TABLE_NAME
            + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " + key_msg + " STRING, " + key_isread + " STRING)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

//*************************-------INSERT GCM MESSAGE---------**************************//

public void insert_GCM_receive_data(String msg) {

    String value;
    SQLiteDatabase db = getWritableDatabase();

    ContentValues cv = new ContentValues();

    cv.put(key_msg, msg);
    cv.put(key_isread, "N");

    value = cv.toString();

    db.insert(TABLE_NAME, null, cv);
    System.out.println("/n******this is temp table name  " + TABLE_NAME + "\nthis is temp msg  " + cv + "\nmsg" + msg + "\nval" + value);

    db.close();

}

//-----------------------------------------------------------------------------------//

//**********************----------GET ALERT DATA------------**************************// public ArrayList get_alert_msg() {

    ArrayList<String> name = new ArrayList<String>();
    try {
        SQLiteDatabase db = getWritableDatabase();
        Cursor c = null;
        c = db.rawQuery("SELECT  * FROM " + TABLE_NAME, null);

        System.out.println(c);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

            String str_id = c.getString(0);
            String str_msg = c.getString(1);
            String str_read = c.getString(2);
            Log.e("value", str_id + str_msg + str_read);

            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("msg", str_msg);
            hm.put("isread", str_read);


            name.add(str_msg);


        }
        c.close();
        db.close();

    } catch (Exception e) {
        Log.e("this not work", "" + e);
    }

    return name;
}

//call method like in 2 activity dbHelper.get_alert_msg(); ....

Data manipulation and retrieve operation use with in the Database class... Once you close the db after retrieve data . Use common database method to two activity.. I hope this will help you..

Akash pasupathi
  • 304
  • 1
  • 14
-1

Make sure to call close() on cursors or db helpers when activity b is done with the updates.

Serg Chernata
  • 12,280
  • 6
  • 32
  • 50
  • This doesn't seem correct to me. See, e.g., http://stackoverflow.com/questions/14267176/closing-the-sqlitedatabase-in-a-content-provider?rq=1 – Hod Jan 07 '17 at 01:47
  • Google's docs say the same thing, kind of. But they still recommend closing in onDestroy. Scroll to the bottom: https://developer.android.com/training/basics/data-storage/databases.html#PersistingDbConnection – Serg Chernata Jan 07 '17 at 12:27