Question: I have a layout in xml.

In my activity, in the onCreate method i get a reference to the view i want to update periodically like this:

TextView myText = (TextView)findViewById(R.id.textview1);

Then, in a periodic method, i do this:

myText.setText(Float.toString(myData));

It works fine, until the screen rotates.

I read that when the screen rotates, the layout is recreated, and all the views on the layout are recreated, so the old references don't work.

My code:

```
private TextView myText; //Its a class member

public void onCreate(Bundle savedInstanceState) {
   //Other things
   myText = (TextView)findViewById(R.id.textview1);
   //Other things
}

//This is the periodic method, it occurs many times per second
public final void onSensorChanged(SensorEvent event) {
   myText.setText(Float.toString(event.myData)); 
   //This setText works fine until the screen rotation
}

```

How can i have a permanent reference to the view?
And how can i know when the screen has rotated?

Thanx

Answer: 
Are you unregistering your sensor listener in your `onPause()`? I think you must be getting sensor events between destruction of your view and the call to onCreate(). (You could try setting some breakpoints to see if this is happening.)

Have a look at [the code on this page](http://developer.android.com/reference/android/hardware/SensorManager.html) for how it should be done.

Otherwise, that code should work. (I do this in my code and it's fine -- your member should be reassigned after the new view is created.) 

You shouldn't need to worry about onConfigurationChanged(), and the Android docs encourage you not to mess with this.
Just use instance variable initialized in onCreate(). If you are lazy performing findViewById(), you can use my small injector library:

<https://github.com/ko5tik/andject>