I'm writing an app which in i have to save the state of the activity. I have a question and some buttons in the activity. When the user click on the correct answer more events happens at the same time.
1.The phone is vibrating.
2.An imageview appears.
3.The buttons become disabled.
I'like to save the imageview and the buttons status but when i click on the back button and open the activity again it's all gone and i have to start from the beginning. I'm using SharedPrefernces to save them but it's not working. I created a boolean type which is false in the beginning. When the user choose the correct answer the boolean become true and if it's true then the events happens. I save this true status in the onStop method and i use a Toast which is show me the state of the boolean. When i choose the correct answer the Toast shows "true" and when i open the activity again it's shows "false". It's obvious that the trying to save the "true" status is bad, but am i using the right type of store to save the imageview and the buttons status? So the thing i am struggling with is to save the status with SharedPreferences.
If anyone has an idea how to do it please response! :)
I copy my code:
private static final String KEYFIRST = "KEYFIRST";
private static final String DB_INIT = "DB_INIT";
String requested_word = "gol";
final Button buttons[] = new Button[3];
final Button betu[] = new Button[3];
final String[] letters = new String[3];
final int[] arr = new int[3];
boolean done = false;
ImageView imageview2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TEST2", "CREATE3");
}
@Override
protected void onStart() {
super.onStart();
Log.d("TEST2", "STRAT3");
SharedPreferences sp = getSharedPreferences(DB_INIT, MODE_PRIVATE);
Boolean state = sp.getBoolean(KEYFIRST, done);
Toast.makeText(getApplicationContext(), ""+done, Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
buttons[0] = (Button) findViewById(R.id.valaszelso);
buttons[1] = (Button) findViewById(R.id.valaszmasodik);
buttons[2] = (Button) findViewById(R.id.valaszharmadik);
betu[0] = (Button) findViewById(R.id.elso);
betu[1] = (Button) findViewById(R.id.masodik);
betu[2] = (Button) findViewById(R.id.harmadik);
letters[0] = betu[0].getText().toString();
letters[1] = betu[1].getText().toString();
letters[2] = betu[2].getText().toString();
imageview2 = (ImageView) findViewById(R.id.imageview2);
for (int i = 0; i < 3; i++) {
arr[i] = 0;
}
betu[0].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < 3; i++) {
if (arr[i] == 0) {
arr[i] = 1;
buttons[i].setText(betu[0].getText().toString());
betu[0].setVisibility(View.INVISIBLE);
break;
}
}
}
});
betu[1].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < 3; i++) {
if (arr[i] == 0) {
arr[i] = 1;
buttons[i].setText(betu[1].getText().toString());
betu[1].setVisibility(View.INVISIBLE);
break;
}
}
}
});
betu[2].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < 3; i++) {
if (arr[i] == 0) {
arr[i] = 1;
buttons[i].setText(betu[2].getText().toString());
betu[2].setVisibility(View.INVISIBLE);
break;
}
if (arr[3 - 1] == 1) {
test();
}
}
}
private void test() {
String word = "";
for (int i = 0; i < 3; i++) {
word += buttons[i].getText().toString();
}
if (word.equals(requested_word)) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
done = true;
Log.d("TEST2","set true");
if(done == true){
for (int j = 0; j < 3; j++) {
buttons[j].setEnabled(false);
betu[j].setEnabled(false);
imageview2.setVisibility(View.VISIBLE);
}
Toast.makeText(getApplicationContext(), ""+done, Toast.LENGTH_LONG).show();
}
}
}
});
super.onResume();
Log.d("TEST2", "RESUME3");
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences sp = getSharedPreferences(DB_INIT, MODE_PRIVATE);
Editor et = sp.edit();
et.putBoolean(KEYFIRST, done);
et.commit();
if(done == true){
Log.d("TEST2","true saved");
}
Log.d("TEST2", "STOP3");
}
}