How to exit an Android app programmatically?

前端 未结 29 974
孤城傲影
孤城傲影 2020-11-28 03:54

I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this

相关标签:
29条回答
  • 2020-11-28 04:15

    It works using only moveTaskToBack(true);

    0 讨论(0)
  • 2020-11-28 04:16
    android.os.Process.killProcess(android.os.Process.myPid());
    
    0 讨论(0)
  • 2020-11-28 04:16
     @Override
        public void onBackPressed() {
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory( Intent.CATEGORY_HOME );
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);
        }
    
    0 讨论(0)
  • 2020-11-28 04:16

    If you are using EventBus (or really any other pub/sub library) in your application to communicate between activities you can send them an explicit event:

    final public class KillItWithFireEvent
    {
        public KillItWithFireEvent() {}
    
        public static void send()
        {
            EventBus.getDefault().post(new KillItWithFireEvent());
        }
    }
    

    The only downside of this is you need all activities to listen to this event to call their own finish(). For this you can easily create shim activity classes through inheritance which just listen to this event and let subclasses implement everything else, then make sure all your activities inherit from this extra layer. The kill listeners could even add some extra functionality through overrides, like avoiding death on certain particular situations.

    0 讨论(0)
  • 2020-11-28 04:18

    It's way too easy. Use System.exit(0);

    0 讨论(0)
  • 2020-11-28 04:19

    Use this.finishAffinity(); on that button instead of finish(); If it does not work then you can also try by adding android:noHistory="true" in your manifest and then finish your activity by uisng finish(); or finishAffinity();

    Hope it helps....:)

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