2

I just read this: Saving Android Activity state using Save Instance State

this: Android - Open resource from @drawable String

My situation is: I have a Button background set when pressing it to be green with

button.setBackgroundResource(R.drawable.greenbutton);

How can I store this information with onSaveInstanceState and onRestoreInstanceState?

I tried: How to maintain a button style and click state after screen rotation? and worked but maybe there is something better than a three-nested-if-conditionals procedure? I mean I have to do this for 4+ Button and I think it a lot of work for just a simple cause :)

Thank you

EDIT: this is the code so far

package com.example.android.testbutton;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.change);
    }

    Button button;
    Boolean click;

    public void changeColor(View view) {
        click = true;
        button.setBackgroundResource(R.drawable.greenbutton);
    }


    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        // Save UI state changes to the savedInstanceState.
        // This bundle will be passed to onCreate if the process is
        // killed and restarted.

        savedInstanceState.putBoolean("buttonClicked", click);
        // etc.

        super.onSaveInstanceState(savedInstanceState);
    }

//onRestoreInstanceState

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);

        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.

        Boolean firstAnswer = savedInstanceState.getBoolean("buttonClicked");
        {
            if (savedInstanceState != null) {
                if (savedInstanceState.containsKey("buttonClicked")) {
                    if (savedInstanceState.getBoolean("buttonClicked"))
                        button.setBackgroundResource(R.drawable.greenbutton);

                    //some codes to make the button becomes clicked.
                }
            }
        }
    }
}
BROKENCODE
  • 85
  • 1
  • 10

2 Answers2

1

set a variable on your activity too, and then save that and restore it with your state.

public class MyActivity extends Activity {

public static final String KEY_BUTTON_IS_GREEN = "isGreen";
boolean buttonIsGreen;

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(KEY_BUTTON_IS_GREEN, buttonIsGreen);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    buttonIsGreen = savedInstanceState.getBoolean(KEY_BUTTON_IS_GREEN, false);
    if (buttonIsGreen){
        // find the button and set it green.
    }
}

you'll need to add to your method that sets the button green and also set your variable = true if the button is green.

MrPlow
  • 1,214
  • 10
  • 9
  • Thank you, but that's what I've done so far, but I was asking if there is a different and more efficient method to pass the variable. I tried **Resources colorFirst = answerOne.getResources();** but then, I can't call _savedInstanceState.putResources_ . The same with **Drawable colorFirst = answerOne.getBackground();** and **int colorFirst = answerOne.getHighlightColor();** – BROKENCODE Dec 05 '17 at 08:10
1

You have a few options:

  1. You can set specific flags in AndroidManifest file:

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>

It does not work by default because , when you change the orientation onCreate will be called again and it redraws your view. But If you set these flags and you are using a different layout for landscape mode, by adding these parameters the layout for landscape mode will not be called, because onCreate will not be called second time.

  1. You can save int resources by:

    int colorFirst = answerOne.getHighlightColor(); savedInstanceState.putInt("key", colorFirst);

mac229
  • 4,319
  • 5
  • 18
  • 24