Hello Together I startet to write my first Android app and I tried to use the SharedPreferences to have the possibility to store some strings.
I can Type in different names and in the onStop() I put them into the SharedPreferences and after that i make a commit. Has someone a solution that Preferences are shown on the next Activity immediatly? Because at the moment I have to switch back to the Activity where I Typed in the names and if I immediatly switch back to the Activity where the names shall be shown they appear.
protected void onStop() {
SharedPreferences.Editor edit = set.edit();
for(int x=0;x<counterM;x++){
edit.putString("playerM"+x, playersMale.get(x));
}
for(int x=0;x<counterF;x++){
edit.putString("playerF"+x, playersFemale.get(x));
}
edit.putInt("counter", counterF + counterM);
edit.commit();
super.onStop();
}
and here is the onCreate() where I load the name…
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
set = this.getSharedPreferences("MY_COUNT",0);
this.setContentView(R.layout.decisionscreen);
Random r = new Random();
int i = set.getInt("counter",1000);
int x = r.nextInt(i);
name = set.getString("playerM"+x, "no Players found");
TextView t = (TextView)findViewById(R.id.nameView);
t.setText(name+" "+i);
dareButton();
truthButton();
}
I hope someone can figure out what my problem is.
Try to put the shared preferences editor stuff in the onPause override. It will be called as soon as you switch the activity.