Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Frank Cameron-Wilson
8,814 PointsShake Detector
Hi, My app seems to detect a shake as soon as the onResume() method is call, so every time you back out of the app then go back into it, it makes the sound and displays an answer. How could i stop this happening?? I've been looking at the ShakeDetector class and thinking maybe change the startTime variable which is 0 to maybe 1 or something. I've also looked at the onResume() method, thinking that a flush or reset( type of method as it doesn't exist for the class) method could be used to get rid of any data store in the accelerometer sensor.
Any ideas anyone??
Thank you in advance.
3 Answers
Lawrence Fergus
1,653 PointsYou could try using a count variable to check whether the shake detection should trigger. Make a class variable like mCount or something. In onCreate() set it to 0. in onPause() set it to 0. When a shake is detected check to see if mCount == 0, if so don't perform action and increase the mCount value. If it is not 0 then perform action.
If this solution always stops your first shake then you could also try stopping the shake detection from happening during the first second of resuming by starting a timer in the onResume() function.
Hope this helps.
Frank Cameron-Wilson
8,814 PointsThank you Lawrence, I've only just got your message. I'll have a muck around with those ideas.
Pablo Prado
Courses Plus Student 1,741 PointsHi guys,
I seem to have the same issue as you Frank. I can solve it with a count variable, but that feels awfully like a weak band-aid. Is the behaviour normal and common for the listener? I think the device should not think it is being called with each resume.
Would you agree? Or should I just put the band aid and look away?
Cheers, Pablo
Frank Cameron-Wilson
8,814 PointsFrank Cameron-Wilson
8,814 PointsCheers again Lawrence, You hit the nail on the head, I set a member variable at the top, then put a if statement before the call to a newAnswer() method and increased the value by 1 outside of the if statement. and then following the activity lifecycle, i then set it to 0 in the onResume() method. Here's the code for anyone who finds this post.