How to quit android application programmatically

前端 未结 30 1951
迷失自我
迷失自我 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:24

    you can your full apps quit. thus

    Intent intent = getBaseContext().getPackageManager()
                 .getLaunchIntentForPackage(getBaseContext().getPackageName());
    intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    

    it will work 100%. Best of luck!

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

    We want code that is robust and simple. This solution works on old devices and newer devices.

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            getActivity().finishAffinity();
        } else{
            getActivity().finish();
            System.exit( 0 );
        }
    
    0 讨论(0)
  • 2020-11-22 03:30

    You can use finishAndRemoveTask () from API 21

    public void finishAndRemoveTask ()

    Finishes all activities in this task and removes it from the recent tasks list.

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

    @Sivi 's answer closes the app. But on return, if you have some child activities, another unfinished activity might be opened. I added noHistory:true to my activities so the app on return starts from MainActivity.

    <activity 
          android:name=".MainActivity"
          android:noHistory="true">
    </activity>
    
    0 讨论(0)
  • 2020-11-22 03:32

    Friends just add this function to exit your application programmatically #java

    public void onBackPressed() 
    {
    
        finishAffinity();
        System.exit(0);
    }
    
    0 讨论(0)
  • 2020-11-22 03:33

    I'm not sure if this is frowned upon or not, but this is how I do it...

    Step 1 - I usually have a class that contains methods and variables that I want to access globally. In this example I'll call it the "App" class. Create a static Activity variable inside the class for each activity that your app has. Then create a static method called "close" that will run the finish() method on each of those Activity variables if they are NOT null. If you have a main/parent activity, close it last:

    public class App
    {
        ////////////////////////////////////////////////////////////////
        // INSTANTIATED ACTIVITY VARIABLES
        ////////////////////////////////////////////////////////////////
    
            public static Activity activity1;
            public static Activity activity2;
            public static Activity activity3;
    
        ////////////////////////////////////////////////////////////////
        // CLOSE APP METHOD
        ////////////////////////////////////////////////////////////////
    
            public static void close()
            {
                if (App.activity3 != null) {App.activity3.finish();}
                if (App.activity2 != null) {App.activity2.finish();}
                if (App.activity1 != null) {App.activity1.finish();}
            }
    }
    

    Step 2 - in each of your activities, override the onStart() and onDestroy() methods. In onStart(), set the static variable in your App class equal to "this". In onDestroy(), set it equal to null. For example, in the "Activity1" class:

    @Override
    public void onStart()
    {
        // RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS
    
            super.onStart();
            App.activity1 = this;
    }
    
    @Override
    public void onDestroy()
    {
        // RUN SUPER | REGISTER ACTIVITY AS NULL IN APP CLASS
    
            super.onDestroy();
            App.activity1 = null;
    }
    

    Step 3 - When you want to close your app, simply call App.close() from anywhere. All instantiated activities will close! Since you are only closing activities and not killing the app itself (as in your examples), Android is free to take over from there and do any necessary cleanup.

    Again, I don't know if this would be frowned upon for any reason. If so, I'd love to read comments on why it is and how it can be improved!

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