I want to know how to close an app safely and totally.
if I don't want to destroy an activity when I touched the BACK button on my phone, I can use moveTaskToBack()
method to make the activity run in background. And now the main activity of the app is visible. Then if I decide to close this app by touching the BACK button, the main activity's onDestroy()
method will be called, but the activity moved to background's onDestroy()
method won't be called, which means the activity is not stopped. So what should I do to close all the activities of that app, no matter it's in foreground or background.
PS:Actually, I got this problem because of this: I have an app which consists of Activity A(Main interface) and Activity B(a control panel). In Activity B, some control status must remain active as long as the app is running, no matter B is in foreground or background, it's to say when I switched from B to A, then return to B, B's content keep same as before I left it, unless I restart the app. At the beginning, B was destroyed every time I pressed the BACK button. Then I follow someone else's advice rewrite the onKeyDown()
method to protect B from being destroyed by BACK, it was useful to retaining B's status because B was never destroyed. So I have no idea to finish or close B when I try to shutdown the app. Maybe I can add an exit button to B, or send a message to the background B to kill itself when I killed A, but I think these two ways too silly.
I tried to use onSavedInstanceState()
to keep B's status, but that method won't be called when BACK is pressed. I also tried SharedPreference
, but status can't be initialized next time I start the app.