Exit from an Android application

Share

Normally, Android applications do not need an “Exit” button because they have a different lifecycle than, let’s say, a Windows application. To exit an application in Android, the user needs to press the home button and that’s it, but the application is not really closed, it’s sent to the background (the user can no longer see it) and the Android system will manage it’s state from there on, closing it only if the system needs more resources. But what if you really want to provide an “Exit” function to the user ? This article presents some of the choices you have available.

As I said earlier, you don’t usually need an exit function for your Android application, but you could add one if you really want to. The reasons for doing something like this are various and I can tell at least one of them : you want to clear some sensitive data when the user “exits” the application.

Close current activity
If you’re developing a really simple application (can’t really see why you would need an exit function in this case) you can exit the application with just a single line of code added to your exit button listener :

 this.finish();

This closes the current activity, witch in some cases might be enough. Beware that if you call this from a child activity, the parent will still be visible. The same outcome is reached if you call

System.exit(0)

In other words this is pretty much useless for a real life scenario as you would have to provide your user with the possibility to exit from any part of the application instead of forcing him to navigate backwards to the root activity and then exit.

Close application and clear stack
This applies to more complex applications and could be used to clear “sensitive” data. The main idea behind this solution is that a child will call an exit method but this method will NOT call finish() on the child itself, instead it will try to start the main activity setting appropriate flags to clear the activity stack, and then it will send a message to the main activity telling it that it needs to exit, when the main activity receives this message , it will do any necessary cleanup and will then exit (this being the first activity in the stack it’s enough to call “finish()”).
I think it sounds more complicated than it really is so let’s look at some code. In your child activity you would have a button (or a menu item or whatever you want), that will allow the user to exit. In the click listener for the button you would add something like this :

	// start main activity
	Intent exitIntent = new Intent(this,IMDMainActivity.class);
	exitIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
	startActivity(exitIntent);
	IMDMainActivity.exitHandler.sendEmptyMessage(0);

So, what you need to do is :

  • create an intent for your main activity;
  • set flags to clear the activity stack;
  • start the main activity intent;
  • send a message to main activity.

Sending messages between activities is done with a handler.

In our main activity, we declare our handler, that will receive messages from all child activities :

public static Handler exitHandler;
 
@Override
public void onCreate(Bundle savedInstanceState) {
/*
 * Code ommited
 */
 
exitHandler = new Handler() {
    public void handleMessage(Message msg) {
	    super.handleMessage(msg);
	    switch (msg.what) {
	    case 0:
                // clear any informations you like here
		IMDMainActivity.this.finish();
		break;
	    }
	}
};

Because we don’t send any other types of messages, it’s ok for the child activities to send an empty message and pass 0 as a parameter (remember the code from the child activity).
That was it, your application will have a decent exit functionality.
Note : Your main activity will start with the flags to clear the activity stack only when the user wants to exit the application explicitly (clicks an Exit button). In any other cases, for example the user receives a phone call when using the application, the application will behave as any other Android application : it will send the current activity to the background (keeping the activity stack) and will resume it when possible (after the call has ended for example).

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close