Reputation: 139
Hello I have 3 activities in my app and I transport data between them with Intent. The problem that I ran into is that Shared Preferences I have on my main activity get change when I returned to the main activity from the 3 activity. I suspect that the problem is that my intent get reboot but I'm not sure. I'm trying to get the data remain the same as it was when i moved to the 3 activity. my app start in activity 1 then its going to the main activity and at last if you click a button its goes to the third activity get some data and returned to the main activity the problem I have is that the data I got form the first activity get restart when I returned from the third activity. when i move data between activities I use intent.
my code to transport the intent activity 1:
Bundle CIW = new Bundle();
CIW.putInt("one", int1);
CIW.putInt("two", int2);
CIW.putDouble("double", double);
Intent a = new Intent(Must.this, Main.class);
a.putExtras(CIW);
startActivity(a);
my code to get the bundles in my main activity (its in mine on create method):
Intent must = getIntent();
Intent name = getIntent();
Bundle CIW = must.getExtras();
Bundle card = name.getExtras();
int1 = CIW.getInt("one");
int2 = CIW.getInt("two");
double= CIW.getDouble("double");
int3 = card.getInt("three");
my Shared Preferences code (on pause):
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putInt("one", Int1); //the rest of the variable
editor.commit();
my Shared Preferences code (on resume):
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
int1 = settings.getInt("one", int1); //the rest of the variable
my code to transport the intent activity 3:
Bundle number = new Bundle();
number.putInt("three", int3);
Intent a = new Intent(Card.this, Main.class);
a.putExtras(number);
Upvotes: 0
Views: 466
Reputation: 156
Your shared preferences file (name) is always the same right?
If you are using shared preferences from different activity/services/intents/... you should use it with mode MODE_MULTI_PROCESS (constant value int = 4). If not, file gets locked and only one process can write it at once!
So when you call shared preferences in multi processes app do it like this:
SharedPreferences preferences = this.getSharedPreferences("myapp",4);
MODE_MULTI_PROCESS is ON on all version till android 2.3, but latter must be called strictly! Offical docs say:
Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. The bit MODE_MULTI_PROCESS can also be used if multiple processes are mutating the same SharedPreferences file. MODE_MULTI_PROCESS is always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions.
Upvotes: 1