2

I am using SharedPreferences to store my data. When my application is running data is been saved to SharedPreferences successfully but when i close my application and try to save data in it via Intent Service nothing happened . no data saved to it :

public class TinyDB {

private  SharedPreferences preferences;
private String DEFAULT_APP_IMAGEDATA_DIRECTORY;
private String lastImagePath = "";

public TinyDB(Context appContext) {
    preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
}


public void putString(String key, String value) {
        checkForNullKey(key); checkForNullValue(value);
        preferences.edit().putString(key, value).apply();
    }
}

I am using its object in onMessageReceive()

public void onMessageReceived(RemoteMessage remoteMessage) {
    tinyDb.putString("key","value");
}

The main point that i want to make sure that i want to save value when app is not running. When app is running everything is fine.

I also want to know what class or Activity is best for initializing the object of TinyDB , and i should make it static or not ?

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
Zaeem Sattar
  • 990
  • 2
  • 12
  • 30
  • You are saying that you are trying to save a key to sharedPrefs while your application is closed. If this is true, you need to make sure that the resources of tinyDB are still alive when you invoke the putString method (the instance of preferences might not function due to the original Context being lost). – David Lev Jul 31 '16 at 19:32
  • yes i know and that is the problem whose answer i want to know – Zaeem Sattar Jul 31 '16 at 19:35
  • Where exactly are you calling `apply()` or `commit()` on the `SharedPreferences`? – Darwind Jul 31 '16 at 19:59
  • @zaeem-sattar see it this what i wrote help you – ceph3us Jul 31 '16 at 20:06
  • @ceph3us yes i will try your answer in a bit then i will let you know – Zaeem Sattar Jul 31 '16 at 20:07

2 Answers2

0

You can instantiate a new instance of TinyDB in your IntentService class with the Context of your Service (assuming that the IntentService is running on the same process as the original Activity). Please note that after performing all its logic, an IntentService is destroyed with all its resource, so the new TinyDB will be destroyed as well.

David Lev
  • 813
  • 5
  • 12
0

try

 public void onMessageReceived(RemoteMessage remoteMessage) {


     // shortcut and valid local variable
     Context ctx = getApplicationContext()
     // get instance of shared preferences  using service/receiver context!
     // keep in mind device protected context / boot aware stuff 
     // https://source.android.com/security/encryption/file-based
     SharedPreferences sp = ctx.getSharedPreferences(preferencesFileName,Context.MODE_PRIVATE);
     // save pref value 
     sp.edit().put(...).commit();


}

*preferenceFileName - if default is your_packageName + _preferences

ps some things to consider:

more hints:

  • do not store Context class objects - as they are and should stay short lived - instead use WeakReference

        // create if you want to hold context reference 
        WeakReference<Context>  wctx = new WeakReference<Context>(context)
        // then get when you want to use context 
        Context ctx = wctx.get();
    
        if(ctx!=null) // context still alive we can use it 
    
ceph3us
  • 7,326
  • 3
  • 36
  • 43