Giridhar94
Giridhar94

Reputation: 39

Exit button in android

I am using intents in my app and i have created an exit button. When the exit button is pressed it closes the current activity only and the remaining activities are still executed. This is my code:

b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);

            }

How to exit the entire app when the exit button is pressed? help me to solve to problem.

Upvotes: 2

Views: 197

Answers (3)

Hamed Gohari
Hamed Gohari

Reputation: 88

you can try this:

b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent myInt=new Intent (Intent.ACTION_MAIN);
Intent.addCategory(Intent.CATEGORY_HOME);
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myInt);
}});

Upvotes: 1

nKn
nKn

Reputation: 13761

Android doesn't introduce a concept of "close everything I've opened in my app and exit cleanly". The right way of doing so is calling finish() on each of your Activities, this way you're telling to the Android SO you want to exit.

If you have just one Activity, simply calling finish() on it will do the trick. However, if you have many and you handle them putting them in the background/foreground, you may want to read this.

However, don't expect finish() will close your app instantly. Even if you call it, Android will keep it in memory for a while just in case the user wants to open it again in a short amount of time, it has its copy in memory and the loading will be faster, but that's the way.

Upvotes: 2

Sardar Behram
Sardar Behram

Reputation: 15

Try out this one

    @Override
    public void onClick(View arg0) {

        System.exit(0);

    }

Upvotes: -2

Related Questions