5

How can I stop my whole App in simple terms? (all activities, services, Threads, etc. simply everything) The life-cycle callbacks (especially onStop() and onDestroy()) should be called.

Google suggests the following possible solution:

  • kill the process: but this wouldn't call the lifecycle callbacks
  • and finish(); but this is only for one Activity:

But is it possible to access this method from outside like:

//Getting all activityies, how?
//For each gotten activity
AcitvityName.finish();
or via or via getParent().finish(); ?

This did not help me: Best way to quit android app?

FD

Community
  • 1
  • 1
felixd
  • 383
  • 2
  • 7
  • 20
  • what are you trying to do? the back button closes an app by standard – JRowan Apr 05 '13 at 10:33
  • @JRowan nope. There's no concept of "closing the app" on android – Marcin Orlowski Apr 05 '13 at 10:36
  • @WebnetMobile Does this mean that it's simply not possible? I thought there was some method... – felixd Apr 05 '13 at 10:41
  • @Devu Soman what do you exactly mean? Intent myintent = new Intent(this[or what context], [and here? home?]); and how do I start this? – felixd Apr 05 '13 at 10:42
  • 2
    @felix there's no point of doing so. Why you need that for? Because if you think you need it, then in 99,99% cases your app is designed in wrong way – Marcin Orlowski Apr 05 '13 at 10:44
  • @WebnetMobile ok, i just thought that apps were supposed to close with the back button – JRowan Apr 05 '13 at 10:47
  • 2
    @JRowan no. If your app follows lifecycles of Android (fragments/activity) etc - then you do not need to bother about being closed or not. You are just going to different state. – Marcin Orlowski Apr 05 '13 at 11:35
  • 1
    System.exit() will terminate for sure your app :D but it will not call callbacks, did you tried to pop all the backstack then call onBackPressed? – sherpya May 22 '13 at 00:08

3 Answers3

2
 @Override 
public void onPause() {        

        if(isFinishing()){
//code to finish() all activitys threads etc.....
}            

    super.onPause();
} 

in onPause() you can check for isFinishing() and then if it is finishing it goes to onDestroy() so you can execute some code youd like

JRowan
  • 6,824
  • 8
  • 40
  • 59
  • But first the onStop() ist called right? So why do I need this? – felixd Apr 05 '13 at 11:44
  • 1
    no onPause is called first and if it is decided that your app is going to be shut down you can check by if(isFinishing()) – JRowan Apr 05 '13 at 12:03
1

Use a broadcast receiver to call all your activities and call finish locally in them, invoking the current lifecycle callbacks.

Broadcastreceiver and Paused activity

http://developer.android.com/reference/android/content/BroadcastReceiver.html

Community
  • 1
  • 1
Sunkas
  • 9,542
  • 6
  • 62
  • 102
1

Android don't allows user close application directly. You may minimize it to background or directly killing process.

But if you want to call some callbacks and release any resources when user remove your application from screen - you may to do following:

Clearing activity stack in onDestroy method (or if you wish in onBackPressed, onHomePressed). It must calls onDestroy methods in all removing activities so you may release any extended resources in onDestroy method of activity which has links on it.

So when user exit from your application - it will remove his activities and release all resources.

QArea
  • 4,955
  • 1
  • 12
  • 22