0

I am using onPause for killing application when it goes in background.

@Override
public void onPause(){
    super.onPause();
    finish();
    System.runFinalizersOnExit(true);
    System.exit(0);
    android.os.Process.killProcess(android.os.Process.myPid());
}  

This has two problems:

  1. Application is still in background.
  2. When I am in Second or Third or next activity and I press Back key, application exits.
    What I really want is to kill the application only when it goes in background.
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • You could override `onBackPressed` and set a flag before the application leaves to check against if you want to only stop against that one problem. – Jay Snayder Oct 16 '14 at 17:21
  • 3
    The principle of killing an app in Android is a kind of a lost cause. – ToYonos Oct 16 '14 at 17:22
  • It is the requirement. – Nitish Oct 16 '14 at 17:23
  • @Nitish ToYonos is correct though. The application kill is really handled by the operating system and there is typically no guarantee that it will be destroyed. But you can do what you can as in what you have shown. This will work in some circumstances. – Jay Snayder Oct 16 '14 at 17:28
  • @JaySnayder : This does not kill the application from background. – Nitish Oct 16 '14 at 17:33
  • There is one more issue I am facing. Can someone help on this. http://stackoverflow.com/questions/26409317/open-connection-only-once-and-read-data-till-it-is-forcefully-closed – Nitish Oct 16 '14 at 17:42
  • Set a boolean when you are about to start a new activity, and check that boolean in your onPause. If the boolean isn't true, kill your app, if it is true you are starting a new activity and don't want to kill your app. But as everyone else said you can't guarantee your app to be killed... it just isn't how the OS handles things. – zgc7009 Oct 16 '14 at 18:19

0 Answers0