• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

"unfortunately app has stopped" when back button pressed

 
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an android app which turns on a sensor on my device and collects data, the data is then sent via http requests and a response is returned back to the app.
However when the back button is pressed it doesn't go back to the home page of the app, instead it crashed with:


unfortunately app has stopped


And I get these errors:


If I press the back button before data gets collected then the app goes back properly to the home screen.

It seems that the ServiceConnection class is still running which is causing the error.

Any suggestions would be great,
Thanks so much!
 
Saloon Keeper
Posts: 6994
164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it seems that pressing the back button somehow interferes with the background network activity. Given that the network activity is on its own thread, that shouldn't happen, as whatever the network access does, has nothing to do (or at least should have nothing to do) with any GUI. So: what does pressing the back button do? Do you have code that runs in that case?
 
Glenda Karen
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks so much for your reply.

The code for when the back button is pressed looks like:


The thread, both for sensor thread and network is started (or stop) by startFlashSensor()

when it is false, the method stops

but the network continues, even the part inside the statFlashSensor method.

So I guess my question now is, how to stop both threads (network and sensor)?
 
Tim Moores
Saloon Keeper
Posts: 6994
164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the code performs a network operation in its own thread, it may not be possible to stop it right away. You need to ensure that -whatever it is doing- has no effect on the app, and that any exception that may occur within is properly caught - which in this case apparently does not happen.
 
Glenda Karen
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much!

I think I only have this one thread running:
 
Tim Moores
Saloon Keeper
Posts: 6994
164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there exception handling in the code part that you left out?

Is interrupt() being called on the thread at an appropriate time, like when the back button is pressed?
 
Glenda Karen
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! We seemed to have solved the problem adding mFlashThread = null; in to the onBackPressed() if statement.

Thanks again for your help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic