How to quit android application programmatically

前端 未结 30 1950
迷失自我
迷失自我 2020-11-22 03:06

I Found some codes for quit an Android application programatically. By calling any one of the following code in onDestroy() will it quit application entirely?

相关标签:
30条回答
  • 2020-11-22 03:18

    This may be very late and also as per the guidelines you're not supposed to handle the life cycle process by yourself(as the os does that for you). A suggestion would be that you register a broadcast receiver in all your activities with "finish()" in their onReceive() & whenever you wish to quit you can simple pass an intent indicating that all activities must shut down..... Although make sure that you do "unregister" the receiver in your onDestroy() methods.

    0 讨论(0)
  • 2020-11-22 03:21

    There is no application quitting in android, SampleActivity.this.finish(); will finish the current activity.

    When you switch from one activity to another keep finish the previous one

    Intent homeintent = new Intent(SampleActivity.this,SecondActivity.class);
    startActivity(homeintent);
    SampleActivity.this.finish();
    
    0 讨论(0)
  • 2020-11-22 03:21

    If you're using Kotlin, the proper way to exit the app and alternative to System.exit() is a built-in method

    exitProcess(0)
    

    See the documentation.

    Number 0 as a parameter means the exit is intended and no error occurred.

    0 讨论(0)
  • 2020-11-22 03:21

    The easiest way I found to quit an application from an activity, without breaking Android's logic and without adding more code in existing activities and passing extras is the following:

    public static void quitApplication (Activity currentActivity) {
        Intent intent = new Intent (currentActivity, QuitApplicationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        currentActivity.startActivity (intent);
        currentActivity.finish ();
    }
    

    the QuitApplicationActivity being:

    public class QuitApplicationActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
    
            finish ();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:22

    Create a ExitActivity and declare it in manifest. And call ExitActivity.exit(context) for exiting app.

    public class ExitActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            finish();
        }
    
        public static void exit(Context context) {
            Intent intent = new Intent(context, ExitActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 03:22

    To exit you application you can use the following:

    getActivity().finish();
    Process.killProcess(Process.myPid());
    System.exit(1);
    

    Also to stop the services too call the following method:

    private void stopServices() {        
        final ActivityManager activityManager = SystemServices.getActivityManager(context);
        final List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(Integer.MAX_VALUE);
        final int pid = Process.myPid();
        for (ActivityManager.RunningServiceInfo serviceInfo : runningServices) {
            if (serviceInfo.pid == pid && !SenderService.class.getName().equals(serviceInfo.service.getClassName())) {
                try {
                    final Intent intent = new Intent();
                    intent.setComponent(serviceInfo.service);
                    context.stopService(intent);
                } catch (SecurityException e) { 
                     // handle exception
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题