How to kill my own Activity - the hard way

后端 未结 8 580
灰色年华
灰色年华 2021-01-31 10:59

So I have my Activity, and on pressing a \"Quit\" button I call Activity.finish(). This effectively closes my application.

The problem: The Dalvik-process of my applicat

相关标签:
8条回答
  • 2021-01-31 11:27

    So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

    Please don't do this.

    The Dalvik-process of my application is still hanging around as a zombie in background.

    The process is kept in a cache, for potential reuse by Android. You can safely ignore it.

    I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

    You probably have a bug in your application, such as calling startService() and failing to call stopService(), or calling bindService() and failing to call unbindService().

    0 讨论(0)
  • 2021-01-31 11:28

    Well, if not only activity, but also the application you want to terminate, you can use

    System.exit(0)

    People say that it's deprecated or bad example, but, well it's pure Java and does work...

    0 讨论(0)
  • 2021-01-31 11:34

    By killing the process you can stop the application completely.from the last screen on back button press you can kill the process by :

    public void onBackPressed() {   
            super.onBackPressed();   
            int pid = android.os.Process.myPid();
            android.os.Process.killProcess(pid);    }
    
    0 讨论(0)
  • 2021-01-31 11:35
    1. Call finish(); on button click
    2. Add this line to onDestroy method:

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

        public void onDestroy() {
            super.onDestroy();
            android.os.Process.killProcess(android.os.Process.myPid());
        }       
    
    0 讨论(0)
  • 2021-01-31 11:39

    By using the ActivityManager we can close the another applications also.

    private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    am.restartPackage("com.jimmy.appToBeClosed");
    

    to use the above code should have the android.permission.RESTART_PACKAGE permissions in manifest file.

    0 讨论(0)
  • 2021-01-31 11:43

    System.exit() instantly unloads whole virtual machine with all native libraries, services and etc.

    0 讨论(0)
提交回复
热议问题