0

I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I believe.

I have an app which listens to an audio stream (using the mediaPlayer object). If I press the Home button it will continue streaming and hide my app. Then, at a later point I can go back to my app and press stop when I'm done. This is what I want. If however I press the Back button, when I later open my app again the app has been redrawn from fresh. Text boxes, buttons, everything has reset like I've just opened the app for the first time so I can't stop my audio stream. Clicking stop does nothing because the app has 'forgotten' it is streaming (the stream runs under a separate handler from the main UI thread, so I'm guessing since its been 'reset' it has lost track of its handlers?).

Why does this happen with the Back button, and how can I stop it?

jwbensley
  • 10,534
  • 19
  • 75
  • 93

1 Answers1

0

Move the streaming functionality into an Android Service. Use an Activity to bind to the Service and to interact with it.

0xf3f
  • 634
  • 1
  • 5
  • 4
  • This still leaves me with a problem, it doesn't have to be this way but I think its a good point to make: I have a button labelled 'start'. When you click it if its text is 'start' it starts the handler in which the stream runs and changes the button text to 'stop'. When you click and the text is 'stop', it stops the stream and changes the text back to 'start'. – jwbensley Jan 07 '11 at 20:20
  • If I press the button and start the stream (and the button text changes to 'stop') I then press Back and as mentioned above, go about my business listening to a stream then later go back to my app to stop the stream, the interface is redrawn and the button says 'start' so I can't stop it? Clicking start starts another stream going, so now theres two running slightly out of time, and then pressing stop just stops this new stream instance, the old one is still running. If I used a service with a single button like this, when I return to the app it would still start 'start' not 'stop', correct? – jwbensley Jan 07 '11 at 20:22
  • Yes this is correct. You have to save the state of your Activity. Or if you use a service, ask the service if it has a running stream. Pressing Back and restarting the Activity will lead to a call to onCreate. So use something like saveInstanceState.getBoolean("HAS_RUNNING_STREAM") – 0xf3f Jan 08 '11 at 12:52
  • Philipp Schwarte, sorry for the late reply, I haven't had a change to implement this yet. Using a service is exactly what I needed in the end, thanks very much! :D – jwbensley Jan 30 '11 at 12:13