Save state of Image Button

699 Views Asked by At

Okay so I have a application that has many Image Buttons, what I currently have working is all the onClick listeners and have the images change when user clicks the image. I read and researched that I can save the state of these image buttons by using onSavedInstance() and onRestore(). This what I have right now, I am only trying this for one Image Button since I did not want to change all if this was not correct.

I also have buttons that move me through each activity, the problem is when I move to another activity then move back the Image button change does not save and the button image goes back to if it was never pressed.

I have a feeling I am missing something really simple because I have watch and read a few tutorials and still cannot get it to work. If can help me understand I would be thankful.

okay problem solved I only tried it on one of my image buttons using shared preferences and putting that into on pause and on resume. I am now able to press back button turn screen and exit application without losing data.

public boolean isPressed;
private boolean isCandyPressed = false;
private static final String CANDY_PRESSED = "CandyPressed";
private SharedPreferences prefs;
private String PrefName = "MyPref";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_desserts);


    vegetablesButton = (ImageButton) findViewById(R.id.vegetabesButton);


    candy = (ImageButton) findViewById(R.id.candy);
    chocolate = (ImageButton) findViewById(R.id.chocolate);
    cookies = (ImageButton) findViewById(R.id.cookies);
    cupcakes = (ImageButton) findViewById(R.id.cupcakes);
    doughnuts = (ImageButton) findViewById(R.id.doughnuts);
    iceCream = (ImageButton) findViewById(R.id.icecream);
    jello = (ImageButton) findViewById(R.id.jello);
    marshMellows = (ImageButton) findViewById(R.id.marshmellows);
    muffins = (ImageButton) findViewById(R.id.muffins);
    pudding = (ImageButton) findViewById(R.id.pudding);
    yogurt = (ImageButton) findViewById(R.id.yogurt);
    vegetablesButton = (ImageButton) findViewById(R.id.vegetabesButton);


    ImageChanger(chocolate, R.drawable.buttongrabchocolate, R.drawable.chocolate);
    ImageChanger(cookies, R.drawable.buttongrabcookies, R.drawable.cookies);
    ImageChanger(cupcakes, R.drawable.buttongrabcupcakes, R.drawable.cupcake);
    ImageChanger(doughnuts, R.drawable.buttongrabdoughnuts, R.drawable.doughnut);
    ImageChanger(iceCream, R.drawable.buttongrabicecream, R.drawable.icecream);
    ImageChanger(jello, R.drawable.buttongrabjello, R.drawable.jelly);
    ImageChanger(marshMellows, R.drawable.buttongrabmarshmellows, R.drawable.marshmellows);
    ImageChanger(muffins, R.drawable.buttongrabmuffins, R.drawable.muffin);
    ImageChanger(pudding, R.drawable.buttongrabpudding, R.drawable.pudding);
    ImageChanger(yogurt, R.drawable.buttongrabyogurt, R.drawable.yogurt);


    setCandyImage();
    candy.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (!isCandyPressed) {
                candy.setBackgroundResource(R.drawable.buttongrabcandy);
                isCandyPressed = true;
            } else {
                candy.setBackgroundResource(R.drawable.candy);
                isCandyPressed = false;

            }
        }
    });
    vegetablesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(Desserts.this, Items.class
            );
            Desserts.this.startActivity(myIntent);
        }
    });

}


protected void setCandyImage() {
    if (isCandyPressed) {
        candy.setBackgroundResource(R.drawable.buttongrabcandy);


    } else {
        candy.setBackgroundResource(R.drawable.candy);
    }
}

void ImageChanger(final ImageButton a, final int b, final int c) {
    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isPressed) {
                a.setBackgroundResource(b);

            } else
                a.setBackgroundResource(c);
            isPressed = !isPressed;


        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    prefs=getSharedPreferences(PrefName,MODE_PRIVATE);
    SharedPreferences.Editor editor=prefs.edit();

    editor.putBoolean(CANDY_PRESSED,isCandyPressed);
    editor.commit();
}

@Override
protected void onResume() {
    super.onResume();
    prefs=getSharedPreferences(PrefName,MODE_PRIVATE);
    isCandyPressed=prefs.getBoolean(CANDY_PRESSED,false);
    setCandyImage();

}

}
1

There are 1 best solutions below

2
On

The state of the button is not preserved when moving through activities, because the activity object is re-instantiated each time, so in order to fix it, you just make it a static variable.

private static boolean isCandyPressed = false;

Hope this solves your problem