I'm new here. I'm french so maybe my english is not very good, sorry for that.
I'm a beginner in Android development, I got to create an app for finish my study.
I have an activity number 1 called VoeuxActivity.java with 8 buttons (and 8 TextViews), they are all VISIBLEs at the beginning, when an user click on one of them button change by INVISIBLE (user can't see the button after clicked on it), when I quit the app and I come back again on my app, the button is always invisible thanks to the SharedPreferences and a member of this forum. But I want now when I clicked on this button named "totoB" it will be invisible and another button becomes Visible on another activity number 2 called PersoActivity.java because the first activity is for unlock some characters locked, when I choose a new character in activity 1, it will be invisible in activity 1 and will be visible on activity 2 in order to choose 2 characters for a fight (that's why there's a boolean name "isClicked") but I tried to use SharedPreferences for stay the button visible on the second activity but it doesn't work at all. When a new character is unlock if I quit the app and come back again in my app, the new character unlock isn't save as visible and he's Invisible again but I want he's visible always by the SharedPreferences. I post a court code with the same button on my first activity then on the second activity (I try to do the same as the first activity but my solution can't work), maybe u could help me to resolve my issue.
The code of the first activity can work and save the changements:
public class VoeuxActivity extends Activity {
Button totoB;
TextView totoTv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voeux);
final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
totoB = (Button) findViewById(R.id.perso1);
totoTv = (TextView) findViewById(R.id.perso1Text);
totoB.setVisibility(prefs.getBoolean("isTotoBVisible", true) ? View.VISIBLE : View.INVISIBLE);
totoTv.setVisibility(prefs.getBoolean("isTotoTVVisible", true) ? View.VISIBLE : View.INVISIBLE);
totoB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prefs.edit().putBoolean("isTotoBVisible", false).apply();
prefs.edit().putBoolean("isTotoTVVisible", false).apply();
totoB.setVisibility(View.INVISIBLE);
totoTv.setVisibility(View.INVISIBLE);
Intent intentToto = new Intent(VoeuxActivity.this, JouerActivity.class);
startActivity(intentToto);
}
});
}
I try to do the same thing for the second activity but it can't work this time, the changement aren't saved.
public class PersoActivity extends Activity {
public static Personnage p1, p2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perso);
final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
final Button totoPersoBtn = (Button) findViewById(R.id.perso1);
final TextView totoPersoTv = (TextView) findViewById(R.id.perso1Text);
totoAdversaireBtn = (Button) findViewById(R.id.adversaire1);
totoAdversaireTv = (TextView) findViewById(R.id.adversaire1Text);
totoPersoBtn.setVisibility(prefs.getBoolean("isTotoPersoBtnInvisible", true) ? View.INVISIBLE : View.VISIBLE);
totoPersoTv.setVisibility(prefs.getBoolean("isTotoPersoTvInvisible", true) ? View.INVISIBLE : View.VISIBLE);
totoAdversaireBtn.setVisibility(prefs.getBoolean("isTotoAdversaireBtnInvisible", true) ? View.INVISIBLE : View.VISIBLE);
totoAdversaireTv.setVisibility(prefs.getBoolean("isTotoAdversaireTvInvisible", true) ? View.INVISIBLE : View.VISIBLE);
if(VoeuxActivity.isClicked) {
prefs.edit().putBoolean("isTotoPersoBtnInvisible", false).apply();
prefs.edit().putBoolean("isTotoPersoTvInvisible", false).apply();
prefs.edit().putBoolean("isTotoAdversaireBtnInvisible", false).apply();
prefs.edit().putBoolean("isTotoAdversaireTvInvisible", false).apply();
totoPersoTv.setVisibility(View.VISIBLE);
totoPersoBtn.setVisibility(View.VISIBLE);
totoAdversaireBtn.setVisibility(View.VISIBLE);
totoAdversaireTv.setVisibility(View.VISIBLE);
} else {
totoPersoBtn.setVisibility(View.INVISIBLE);
totoPersoTv.setVisibility(View.INVISIBLE);
totoAdversaireBtn.setVisibility(View.INVISIBLE);
totoAdversaireTv.setVisibility(View.INVISIBLE);
}}}
How can I save the changement of the Button and the TextView from Visible to Invisible in the second activity? Thank u very much if someone can helps me because I really don't know why it's not work at all. Giggs