0

I've got a fragment where I'm recording audio, I want to pause the recording when a user presses the home key, but continue to record on a rotation.

In onPause, onStop or some other method that gets called, how can I differentiate between when the user has pressed home, or whether the fragment is being closed down because of a rotate?

I've looked into listening for the home key press, but everything I find says that isn't supported/won't work/can't do it.

I've tried using an OrientationEventListener, but it seems to get called nonstop, and if I'm looking for 0, 90, 180, and 270, my code still gets run if orientation goes from 1 to 0.

There has to be some way to differentiate that!? Any ideas?

EDIT is this my best option? --> http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

EDIT

When I press the home button, I need to stop recording in onPause or onStop. When I rotate the device, I need to continue recording through those methods. So I need something I can check in those methods or before that tells me whether this is a home press or a rotation.

SOLUTION

I ended up just locking the orientation of the fragment when it's started and not allowing rotation. Not ideal, but more ideal than any other solution.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
joshkendrick
  • 3,497
  • 8
  • 38
  • 52
  • you can mark Fragment(which you are using for Audio record) as "retain-able" by using `setRetainInstance(true)`. When the Android system shuts down your activity due to a configuration change, the fragments of your activity that you have marked to retain are not destroyed. This way, you can continue Audio recording and when HOME Key is pressed, onPause() is surely going to be called where you can stop Audio recording. – AADProgramming Mar 06 '15 at 16:46
  • I have setRetainInstance(true) on the fragment already. Doesnt help in this case because most of the fragment lifecycle still executes with that set to true. – joshkendrick Mar 06 '15 at 16:53
  • When HOME is pressed, are you able to pause Audio in onPause()? If so, when device config. changes on rotation and since you are already retaining the fragment state, I believe it should continue playing Audio along with its lifecycle methods being executed...isn't it? – AADProgramming Mar 06 '15 at 17:17
  • I can pause Audio in onPause. My problem is if I pause Audio recording in onPause, then it will pause on a rotation change and i dont want it to. – joshkendrick Mar 06 '15 at 19:56

0 Answers0