Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : when to unregister the listener - onStop() or onDestroy()

Tags:

android

I have basic question regarding when to unregisterListener for sensor manager. Should it be done in onStop() or onDestroy().

Usecase :

I want to record accelorometer on click of start button and stop when user clicks stop button. the frequency of data is every minute. So I have started a timer.

But the issue is every time the orientation changes as per Android architecture, onStop() gets called. In onStop() I am cancelling the timer and unregistering the listener.

SO again if I start timer/register listener in onResume() the frequency won't remain 1 minute and also the data gets recorded without user pressing the start button.

Can someone help me resolve this issue.

Thanks.

like image 461
Android_enthusiast Avatar asked Jan 25 '11 03:01

Android_enthusiast


1 Answers

You may want to use a Service to run the accelerometer data collecting in the background, communicating with it using basic intents is not too complicated.

Or, if you only want to 'survive' the rotation, when the activity gets destoryed and then rebuilt, try overriding in your Activity the onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods (read more about it here)

like image 79
Alex Biro Avatar answered Sep 23 '22 16:09

Alex Biro