How to exit from the application and show the home screen?

后端 未结 20 1999
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:45

I have an application where on the home page I have buttons for navigation through the application.

On that page I have a button \"EXIT\" which when clicked should t

相关标签:
20条回答
  • 2020-11-22 10:15

    Add following lines after finish(); in onDestroy():

    android.os.Process.killProcess(android.os.Process.myPid());
    super.onDestroy();
    
    0 讨论(0)
  • 2020-11-22 10:17

    100% works fine. this is code for Exit your app onClick (Method)

        Button exit = (Button)findViewById(R.id.exitbutton);
    
        exit.setOnClickListener(new View.OnClickListener() {
    
            @Override
    
            public void onClick(View view) {
    
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
                Toast.makeText(getApplicationContext(), "Closed Completely and Safely", Toast.LENGTH_LONG).show();
    
            }
        });
    
    0 讨论(0)
  • 2020-11-22 10:19

    May be you can try something like this

    Suppose in our application, we have a number of activities(say ten) and we need to exit directly from this activity. What we can do is, create an intent and go to the root activity and set flag in the intent as

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    also, add some extra like boolean to the intent

    intent.putExtra("EXIT", true);
    

    Then in root activity, check the value of the boolean and according to that call finish(), in the onCreate() of the root activity

    if (getIntent().getBooleanExtra("EXIT", false)) {
     finish();
    }
    
    0 讨论(0)
  • 2020-11-22 10:19

    If you want to exit from your application. Then use this code inside your button pressed event. like:

    public void onBackPressed()
    {
        moveTaskToBack(true);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);
    }
    
    0 讨论(0)
  • 2020-11-22 10:20

    if you want to exit application put this code under your function

    public void yourFunction()
    {
    finishAffinity();   
    moveTaskToBack(true);
    
    }
    
    
    
    //For an instance, if you want to exit an application on double click of a 
    //button,then the following code can be used.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 2) {
            // do something on back.
            From Android 16+ you can use the following:
    
            finishAffinity();
            moveTaskToBack(true);
        }
    
        return super.onKeyDown(keyCode, event);
    }
    
    0 讨论(0)
  • 2020-11-22 10:20

    You can just add moveTaskToBack(true) in your exit button's onClickedListener to minimize the application.

    Hope it helps.

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