2

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.

Wtswkz
  • 333
  • 1
  • 3
  • 12

3 Answers3

1

From back stack doc:

When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the Back button. As such, the back stack operates as a "last in, first out" object structure. Figure 1 visualizes this behavior with a timeline showing the progress between activities along with the current back stack at each point in time.

Moreover if you need to close your application fully (all foreground + background activities), you can kill the process at any point in your application. Use,

android.os.Process.killProcess(android.os.Process.myPid());

You will get some details information below,

How to kill an application with all its activities? How can I close my application on back pressed in android

Kill Activity on back button

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • Thank you! I've read these things before. But I always get problems in practice. What I want to know isn't how to kill an Activity on Back button, it's :when I moved an Activity to background by using Back button, and the Activity is not visible, then how can I destroyed the activity the time I try to close the whole app without going back to the Activity and pressing an exit button or item. – Wtswkz Jan 07 '16 at 14:27
  • I tried the way you added, and it really killed all the activities of the app. I know this because I know that B's `onRestoreInstanceState()` method (which won't be called if I do not use `killProcess()` method) was called. Therefore, every time I restart the app, B is same as before I stop the app. – Wtswkz Jan 08 '16 at 11:24
  • Actually, this effect is not what I want. But I've got solutions to get B's content can and only can initiate when I restart the app. Such as using `Intent` and 'Bundle` to make Activity A stores B's status(without `killProcess()`), or make status cannot be stored by `onStoreInstanceState()`(with `killProcess()`). – Wtswkz Jan 08 '16 at 11:31
  • Anyway, your answer can solve my question. Thank you! – Wtswkz Jan 08 '16 at 11:32
0

There is no guarantee that ondestroy() is called every time of activity. So I think it should be better to finish your activity in onStop() method because it's called every time.For more please refer this

Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15
-1

In order to finish activity completely i.e. not run in foreground as well in background, you just need to call Activity method finish(). This method finishes the activity. If you want to finish activity on back Press button. use below code

public void onBackPressed(){
    this.finish();
}
Dev
  • 13,492
  • 19
  • 81
  • 174
Deepak
  • 756
  • 4
  • 10