0

I have two apps, App1 and App2. I,m saving some data in App2 and accessing in App1. So when i,m coming back to App1 from App2 using backpress, shared pref data will not refresh that i,m accessing from App2. On removing App1 from background and coming back to same page will do the work.

So, what should I do such that, Shared Pref Data in App1 will fetch the latest data I,ve stored in App2 ?

Pratik
  • 39
  • 5
  • @HRaval here : http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application – Pratik Mar 02 '16 at 06:32
  • @HRaval If the preference is WORLD_READABLE, sharing between apps will work. Have a look at this post: http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application – Jayakrishnan Salim Mar 02 '16 at 06:33
  • I,m able to share data between two apps, but share pref should refresh and fetch the latest data that I stored in App2,when I back press and go to app1. – Pratik Mar 02 '16 at 06:36
  • where you are accessing the SharedPreference in App1? Also how you started App2 from App1? – sanky jain Mar 02 '16 at 06:40
  • @sankyjain: I,m going to App2 from App1 on pressing a button in App1. When in App1, I,m storing some values in Shared pref and OnBackPress, Coming back to App1. Then in App1, i,m accessing the data that I stored in shared pref in App2. But changes are not reflecting in App1 until App1 is removed from background and App1 is restarted. – Pratik Mar 02 '16 at 07:16
  • are you updating the screen on onResume()? – Pooya Mar 02 '16 at 07:19
  • @Pooya : Yes i,m updating the screen on onResume(). – Pratik Mar 02 '16 at 07:53

2 Answers2

0

When writing data to the shared preference use edit.apply() instead of edit.commit(), as edit.apply will update the preference object instantly and will save the new values asynchronously, so allowing you to read the latest values.

Sid
  • 1,224
  • 3
  • 23
  • 48
  • Its not working. Maybe its updating shared pref in App2 instantaneously but I need changes to reflect in App1 immediately, when I go to App1 from App2 on BackPress. – Pratik Mar 02 '16 at 07:09
  • Can you share the code that you have at both places, that will be very helpful in debugging. – Sid Mar 02 '16 at 10:08
  • This is how i,m doing it : http://stackoverflow.com/questions/35652147/how-can-i-share-data-between-two-android-apps-using-shared-preferences/35652560#35652560 – Pratik Mar 04 '16 at 06:27
  • There is the mistake, just replace edit.commit() with edit.apply(). This should be your code: SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_PRIVATE); SharedPreferences.Editor editor =prefs.edit(); editor.putString("demostring", strShareValue); editor.commit(); – Sid Mar 04 '16 at 07:24
  • Yes I tried edit.apply, But its also not working. Actually Problem is I need to remove the App1 from background and Start again in order to changes to reflect ! – Pratik Mar 08 '16 at 08:15
0

Your unable to change sharePreference data of other, you able to use ContentProviders these are a good approach to share data between applications, while within application you can easily access store data (of sharePreference) get more detail from This Link

Community
  • 1
  • 1
Attaullah
  • 3,856
  • 3
  • 48
  • 63