Q:

How to save a button across a configuration change?

In my Activity, I have a field that is a button. It's value is always the currently selected button in a LinearLayout that is created dynamically in the activity. I want this field to always point to the button that is currently selected, however, if I select a button and then change the configuration, the field no longer points to the selected button. What can I do so that I can save this value across a configuration change? 
Here's my code:
Activity.java
public class Activity extends AppCompatActivity {
    Button myButton;
    LinearLayout myLayout;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        for (int i = 0; i < 2; i++) {
            LinearLayout row = new LinearLayout(this);
            LayoutParams rowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f);
            row.setLayoutParams(rowParams);
            for (int j = 0; j < 2; j++) {
                final Button button = new Button(this);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setMyButton(button);
                    }
                });
            }
            row.addView(button);
        }
        myLayout.addView(row);
    }

    public void setMyButton(Button button) {
        myButton = button;
    }
}

I have looked into using some savedInstanceState methods but none of them have one for a Button, only integers and booleans.
What can I do so that I can retain the value of myButton when the configuration changes?

A:

You can use onSaveInstanceState() as long as you have some way to identify the button you want to save; you don't have to save the button itself.
Normally, you'd use the button's id for this purpose, but since you're dynamically creating the buttons at runtime (instead of putting them in your XML layout) I recommend using the button's tag instead.
Save the button's tag in onSaveInstanceState(), and then when you're creating the buttons, if you create a button with that same tag, save it as myButton.
onSaveInstanceState():
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("myButtonTag", myButton != null ? (String) myButton.getTag() : null);
}

At the top of onCreate():
    String myButtonTag = null;

    if (savedInstanceState != null) {
        myButtonTag = savedInstanceState.getString("myButtonTag");
    }

Inside your inner for loop:
            final Button button = new Button(this);
            String tag = i + "," + j;
            button.setTag(tag);

            if (tag.equals(myButtonTag)) {
                myButton = button;
            }

