SciLor's Open Source Forums

Also visit my Website!
It is currently Wed Nov 21, 2018 4:01 am

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Android - Exit Button
PostPosted: Sun Apr 24, 2011 10:33 am 
Offline
The Developer

Joined: Wed Jan 13, 2010 11:56 pm
Posts: 1467
Donated: free software
I needed a while to find a soltion to implement an exit button in an Android app. All you can find in the internet is: "There is no need", "It is not intented" and so on.

But sometimes you really want to exit the application. Here is the solution:

First of all create following onActivityResult method on all of your activities:
Code:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 99) {
            setResult(99);
            this.finish();
        }
}

Then you will need to run all of your activities in following way:
Code:
this.startActivityForResult(intent, 99);

And just do following to close the app:
Code:
setResult(99);
this.finish();


In addition you could also do following:
Code:
@Override public void onWindowFocusChanged(boolean visible) {
       if (closeAll) {
            setResult(99);
            this.finish();
       }
}

You will also need to declare following in all of your activities:
Code:
private static boolean closeAll;

To complete it change your "exit" button to following:
Code:
closeAll = true;
setResult(99);
this.finish();

_________________
My Windows Mobile and Windows Open Source Website:
www.scilor.com

Like my work? Donate!
Donation Website

My Programs:
grooveshark™.com Downloader, GrooveMobile, Fuel Blaster, Chameleon TicTacToe, QuickTap, WiMoBlue, Driving Licence Trainer, CodeSnippetViewer, Gesture Launcher, Fahrplanauskunft, GSensor Control


Top
 Profile  
 
 
Online
The Adswinger

Joined: Wed Jan 13, 2010 11:56 pm
Posts: Too many!
Donated: Ad Posts


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
template->_php_include(): File ./counter/foruminclude.php does not exist or is empty Powered by phpBB® Forum Software © phpBB Group