How to exit an Android app programmatically?

前端 未结 29 975
孤城傲影
孤城傲影 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:28

    Just run the below two lines when you want to exit from the application

    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);
    
    0 讨论(0)
  • 2020-11-28 04:29

    You can call System.exit(); to get out of all the acivities.

        submit=(Button)findViewById(R.id.submit);
    
                submit.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View arg0) {
    android.os.Process.killProcess(android.os.Process.myPid());
                        System.exit(1);
    
                    }
                });
    
    0 讨论(0)
  • 2020-11-28 04:29

    this will clear Task(stack of activities) and begin new Task

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    System.exit(1);
    
    0 讨论(0)
  • 2020-11-28 04:29

    in the fragment

    getActivity().finishAndRemoveTask();

    in the Activity

    finishAndRemoveTask();

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

    Actually every one is looking for closing the application on an onclick event, wherever may be activity....

    So guys friends try this code. Put this code on the onclick event

    Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory( Intent.CATEGORY_HOME );
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
        startActivity(homeIntent); 
    
    0 讨论(0)
提交回复
热议问题