Ganjira
Ganjira

Reputation: 976

Closing an Android app

I have a simply question. I set up the button which is closing my app:

Button turnoffbutt = (Button) findViewById(R.id.button3);
turnoffbutt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        System.exit(0);
    }
});

It's working very good, but only when I turn on my app. When I open another activity by a button

startActivity(new Intent(MenuActivity.this, SettingsActivity.class));

and then I go back to the menu I can't quit my app (by this button Close). It's getting me to the settingsactivity. I know that this method (startactivity) is creating a new activity. That's why I can't quit my app. But how to resolve this problem? May I have to change something on turnoffbutt or use another method to get to the another activity?

Thanks in advance!

Upvotes: 1

Views: 213

Answers (2)

Aleks G
Aleks G

Reputation: 57306

Normally, you should not explicitly "quit" or "close" your app, as that's not how Android is designed. However if you really want to do something like that, you can just finish the activity, something like this:

Button turnoffbutt = (Button) findViewById(R.id.button3);
turnoffbutt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        finish();
    }
});

Upvotes: 2

Droidman
Droidman

Reputation: 11608

call finish() before starting another Activity. That will make your app close when you hit the back button in your SettingsActivity

I suggest you to read this article for better understanding the things you are asking about

Upvotes: 0

Related Questions