Reputation: 1469
In my activity, i monitor a sensor, with "onSensorChanged" method.
I start monitoring the sensor when a button in that activity is pushed (in the "onClick" method).
I register and unregister the listening in the onPause and onResume methods.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
//Here i set "mySensor"
}
public void onClick(View view){
sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_FASTEST);
}
protected void onPause() {
sensorManager.unregisterListener(this, mySensor);
}
protected void onResume() {
sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_FASTEST);
}
public final void onSensorChanged(SensorEvent event) {
//do things
}
The problem is that, when there is an screen orientation change, the monitoring stops, the things i do within "onSensorChanged" stop until i press the button again.
I have been reading, and it seems that when the screen orientation changes, my activity is destroyed and recreated, but i would like the monitoring of the sensor not stopping during that process.
How can i keep the monitoring? i think it must be done using the Bundle, but im not sure.
Thanx.
Upvotes: 1
Views: 985
Reputation: 7562
You have a few choices:
You can move your sensor listener into your application class, defined by android:name
in <application>
in your manifest. See here, naming a class that you've derived from android.app.Application. I would recommend this approach.
You can add android:configChanges="orientation"
to your activity manifest but, as described here, you'll need to handle the reloading of appropriate resources, such as orientation specific layout, in the onConfigurationChanged()
of your activity. I wouldn't recommend this as you would still have the issue of the monitoring disappearing when the other configuration changes occur or you will need to work out how your application should handle them (and add them to the list of changes in android:configChanges
).
Disabled orientation changes entirely for your application by using android:screenOrientation
as described here, but I really wouldn't recommend this unless there are other reasons why you want your application to only work in a certain orientation.
Upvotes: 1
Reputation: 185
Indeed, Activities on Android do get destroyed and recreated when the orientation of the device changes. If you do not care for all orientations, you can 'limit' your activity a single orientation.
Alternatively, if you wish to provide support for more than one orientation (the default), you can move the sensor reading part either into a static singleton (that you would call from whichever Activity you wanted), into a subclass of the Application class, or a separate class, referenced by a subclass of the Application class) that you could call with getApplication()
— read this for more information about the Application class). All three alternatives are pretty decent choices for keeping global state across Activities in general.
A somewhat more robust way to do it, if you wish to keep reading sensor data even when the application is in the background or after your application is terminated, is to use a Service and pass data back and forth with your activities when the app is running.
Upvotes: 0