-1

I have one app which is using different build variant(flavor), Now I want to share data between these flavored apps(different apps for End User). Now my question is that how can I use shared preferences to share some data amongst the flavored apps as only one package name mentioned in the manifest file and that one package is valid for all the flavored apps. I have tried the below approach and failed to share data.

//To set data

SharedPreferences settings = getSharedPreferences(MyPref, Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("UserEmail", m_email);
editor.putString("UserPass", m_pass);
editor.putString("LoggedUserName", m_statusData);
editor.commit(); 

//To Get data

Context con = getApplicationContext().createPackageContext("my_packagename", 0);
SharedPreferences pref = con.getSharedPreferences(MyPref, Context.MODE_PRIVATE);
EmailId = pref.getString("UserEmail", email);//got always null
Password = pref.getString("UserPass", Password);//got always null

But it is not working. What to do now?

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Absar Alam
  • 102
  • 2
  • 9

2 Answers2

0

Not enough reputation to comment so i post it as an answer...

I guess this is what you are looking for:

How can I share a SharedPreferences file across two different android apps?

you need to use android:sharedUserId in your Manifest.

woodii
  • 763
  • 7
  • 23
0

While reading Word readable data shared by first app, we should create shared pref object like-

Replacing

getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

with

getSharedPreferences("PREF_NAME", Context.MODE_MULTI_PROCESS);

in the second app to get updated value in the second app.

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51