0

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

Alex
  • 1,449
  • 4
  • 18
  • 28
  • What do you mean with "old references don't work"? Do you get an exception? – Korbi Jan 21 '12 at 16:47
  • In the code up there, "myText" stores a reference to the TextView. But when the screen rotates, that TextView is destroyed, and other TextView replaces it in the new layout (created when the rotation occurs), so myText now references a nonexisting TextView. – Alex Jan 21 '12 at 16:50
  • But where do you set the `myText` member variable? In your example code you're setting a local. Can you show us more code? – Martin Stone Jan 21 '12 at 16:53
  • i will edit the question with more code, sorry :P – Alex Jan 21 '12 at 16:56

3 Answers3

1

You might be thinking about things a little incorrectly if you want a permanent reference. The view is removed from memory when the phone is rotated (assuming you allow rotation.)

At the point of rotation you will need to get a pointer to the newly created object.

To detect orientation changes related to layout use onConfigurationChanged as shown in this post.

How to detect orientation change in layout in Android?

Community
  • 1
  • 1
madmik3
  • 6,975
  • 3
  • 38
  • 60
1

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 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.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53
  • Yes, the sensorListener management i think im doing it right, with the onPause and onResume methods, the problem was just the reference to the View, but i solved it with the onConfigurationChanged. How do you think it sould be done? I want to learn how to do it right, for now it works, but if you think it sould be done other way, tell me please :) – Alex Jan 21 '12 at 17:15
  • When you handle this event yourself, you are preventing the view recreation altogether. You have hidden a bug, not fixed it. This failure could still occur when your app restarts for other reasons (e.g. task switching with low memory). The docs say handling onConfigurationChanged() yourself ["should be considered a last resort"](http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange). – Martin Stone Jan 21 '12 at 17:25
0

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

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35