Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
When I start my android-activity first
onCreate()
is called, then
onResume()
.
When I then press the back-button to return to the home-screen and tap the app-icon again, first
onCreate()
is called, then
onResume()
and then
onDestroy()
.
My app is still active then, but doing some action result in error since
onDestroy()
sets a few members to null.
Why is that?
Update:
When I wait 30 seconds after pressing back everything works fine. I'm not doing anything heavy in onDestroy except setting a few variables to null and
.interrupt()
ing a background-thread.
–
–
onDestroy
gets called because, by default, pressing back key results in your activity calling
finish()
which initiates the destroying of the activity which calls onDestroy().
To prevent doing some action in case the activity is being destroyed do like this:
if(!isFinishing()) {
// do your action here
isFinishing is a method of the Activity.
–
I think there is something in addition to what you are describing. Android doesn't just keep activity from being destroyed, something MUST be happening on the main thread.
The symptoms sound exactly as if you had either:
a service doing a longish HTTP or database operation. Are you sure there are no suxg things?
another thread (perhaps managed by an AsyncTask?) calling a synchronized method
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.