0

I've following problem: I've create custom Android class for CheckedTextView:

public class CustomCheckedTextView extends CheckedTextView {
    public CustomCheckedTextView(Context context) {
        super(context);
                this.setOnClickListener (new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ((CheckedTextView) v) .toggle();
                        if (isChecked()){
                            setBackgroundColor(Color.GREEN);
                        } else {
                            setBackgroundColor(Color.RED);
                        }
                        }

            }) ;
        }
}

And use it in main activity as following:

 LinearLayout llayout = (LinearLayout) findViewById(R.id.linearLayout1);
 final CustomCheckedTextView checkedTV = new CustomCheckedTextView(this);
 llayout.addView(checkedTV)

So I can tap on CustomCheckedTextView and background will be Green. But when I rotate phone background become white again. Why it's happens?

Laser
  • 6,652
  • 8
  • 54
  • 85
  • when you're rotating device `onResume()` is called so you can write code in that also.. – Juned Jun 24 '13 at 08:45
  • @juned so I need to check all states in some array, and set them back after each rotation? Is there any another solution? – Laser Jun 24 '13 at 08:49
  • you can go with blganesh101's answer :) – Juned Jun 24 '13 at 08:54
  • while blganes101's answer will do the trick in 80%, sometimes things will go terribly wrong, for reason google states that it should be used as last option possible and to prefer savedInstanceState and restoreInstanceState.. check answers here http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation – Marko Niciforovic Jun 24 '13 at 09:18

2 Answers2

1

Do not use configChanges for this. Having an understanding of why it happens and how to save state is very important. Please read the documentation on this topic.

Jacob Tabak
  • 8,044
  • 3
  • 24
  • 32
-1

Add this to your AndroidManifest.xml file

android:configChanges="orientation"
Juned
  • 6,290
  • 7
  • 45
  • 93
blganesh101
  • 3,647
  • 1
  • 24
  • 44
  • [Documentation states](http://developer.android.com/guide/topics/manifest/activity-element.html): Note: Using this attribute should be avoided and used only as a last resort. Please read [Handling Runtime Changes](http://developer.android.com/guide/topics/resources/runtime-changes.html) for more information about how to properly handle a restart due to a configuration change. – Jacob Tabak Dec 27 '13 at 16:56