Search code examples
androidactivity-finish

How to close app using exit and back button


In my app I have menu page that contains 4 buttons History, Types, Benefits and Exit. After Splash Screen menu page opens. If I start History, Type or Benefit activity. I am not finish menu activity because if I finish this then on press on navigating up icon in action bar app close. When I am in any of the 3 activities and press device back button then I came back to menu activity. And if then I press exit or back button from device. EndSplash starts and app finish thats I want. But after one second app restart. How to close app on exit and back button?

I also have navigating tabs below action bar. Minimum API level 8.

In menu activity-

exit = (Button) findViewById(R.id.exit);
        exit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                i = new Intent(MainActivity.this, EndSplash.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                startActivity(i);
                finish();
            }
        });
@Override 
    public void onBackPressed() {
        super.onBackPressed();
        i = new Intent(MainActivity.this, EndSplash.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i);
        finish();
    } 

And in History,Types and Benefits Activity-

public void onBackPressed()  
    {    
    startActivity(new Intent(this, MainActivity.class));  
        finish();
    }

Solution

  • Try this one to Exit the application:-

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);