How to Close Your Android Application on a Button Click

It’s a common thing to have a button in your application that will close and exit it, especially if you are used to Windows. In Android, however, it would normally be the operating system itself that takes care of such things and allocating or deallocating resources for an app. If you feel you need to implement this and let the user interrupt/unload your application by clicking on a button within your application, you can do that via the following code (let’s assume your button ID is buttonExit):

Button buttonExit = (Button)this.findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(
	new View.OnClickListener() {
		public void onClick(View v) {
			System.exit(0);
		}
	}
);

Of course, if you have use for exit codes, you can substitute the 0 with whichever exit code you would like.

This entry was posted in Android and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *