1

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");
}

}

Endi Tóth
  • 221
  • 3
  • 15

1 Answers1

0

When you press back button, the default action it perform is to finish your activity.

So if you are using sharedPreferences (personally i prefer to use SqliteDB).

Your problem is maybe because you are not specifying your Aplication Context, so i recommend you to change this lines:

SharedPreferences sp = getSharedPreferences(DB_INIT, MODE_PRIVATE);
Editor et = sp.edit();
et.putBoolean(KEYFIRST, done);
et.commit();

To this:

        SharedPreferences  sp=getApplicationContext().getSharedPreferences(DB_INIT, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sp.edit();
        edit.putBoolean(KEYFIRST, done);
        edit.commit();

and this lines:

SharedPreferences sp = getSharedPreferences(DB_INIT, MODE_PRIVATE);
Boolean state = sp.getBoolean(KEYFIRST, done);

to this:

SharedPreferences shared = getApplicationContext().getSharedPreferences(DB_INIT, Context.MODE_PRIVATE);
        response = (shared.getBoolean(KEYFIRST, done));

Your Error is:

@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();
}

You are showing in your Toast the var Done, that originally is false, you must show the state var....

as it:

@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(), ""+state, Toast.LENGTH_LONG).show();

}

Basically the problem is the context that you are saving SharedPreferences. Regards.

Max Pinto
  • 1,463
  • 3
  • 16
  • 29
  • Thank you for your answer! Unfortunately your code it's not working for me. It's the same when the answer is ok the toast shows me true and when i open it again the boolean is false and the imageview disappeared. Can you tell me please why are you prefer SqliteDB? I read that it's used for struct data. – Endi Tóth Jul 09 '15 at 20:43
  • ok, why doesnt work, does it give you some error? if it, could you share your logcat to check, the code i share you i am using in some projects and works, so lets check why not – Max Pinto Jul 09 '15 at 21:39
  • Check this solution too: http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Max Pinto Jul 09 '15 at 21:46
  • I found your error, check the last lines of my answer, there i will you the error – Max Pinto Jul 09 '15 at 21:48
  • You are right i changed done to state in the Toast and now when i open the activity again it's true. But why my pictures gone? I should use another type of store? Do you think it would be better if i use onSaveInstanceState or SqliteDB than SharedPreferences? – Endi Tóth Jul 11 '15 at 09:33
  • I dont see how are you saving your image, you should save the path or base64 like you save the state – Max Pinto Jul 11 '15 at 13:58
  • Sorry for my ignorance but i don't know how to do that. I thought if i save the true condition then the others will be saved too. Could please tell me how to save my image? What is base64? I swear this is my last question! – Endi Tóth Jul 11 '15 at 15:15
  • Base64 is a type of encoding and converting images to strings... So you convert your image as string and save it in shared preferences and then get it and revert base 64 decode again.... That a way, is the hard way. The easy way is save the path of your image aa string in shared preferences and then get that as string... Thats the easy and i think in your case best way. – Max Pinto Jul 11 '15 at 23:22
  • Ok i will do it with the easy way you said! Thank you very much for helping me! – Endi Tóth Jul 12 '15 at 09:35