安卓:当注销监听器 - 的onStop()或的onDestroy() [英] Android : when to unregister the listener - onStop() or onDestroy()
问题描述
我有一个关于何时 unregisterListener
传感器经理基本问题。如果它在的onStop()来完成
或的onDestroy()
。
I have basic question regarding when to unregisterListener
for sensor manager. Should it be done in onStop()
or onDestroy()
.
用例:
我想记录 accelorometer
上启动按钮点击时停止用户点击停止按钮。数据的频率为每分钟。所以,我已经开始了一个定时
。
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
.
但问题是每次定位
变化每Android的架构,的onStop()
被调用。在的onStop()
我取消计时器并注销监听器。
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.
如果我启动定时器/在 onResume注册监听器()
的频率将不会保持1分钟,也得到的数据记录,无需用户pressing的如此反复启动按钮。
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.
感谢。
推荐答案
的OnDestroy()
不能保证被调用。所以,你应该这样做在任何的onPause
或的onStop
OnDestroy()
isn't guaranteed to be called. So you should do it in either onPause
or onStop
但问题是每一个方向改变为根据Android架构的时间
But the issue is every time the orientation changes as per Android architecture
有这个从清单发生的一种方法prevent。我敢肯定,别人会怎么发布
There's a way to prevent this from happening in the manifest. I'm sure someone else will post how.
这篇关于安卓:当注销监听器 - 的onStop()或的onDestroy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!