2

When User click "Exit" button, Its go to device home screen.Then again click/select my application its go to last opened activity.I houldn't go there.It need to go initial screen.How we can implement this.I have done using :

public void onExitAction(View botton){
    SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_WORLD_READABLE);
    myPrefs.edit().remove("myLogedPrefs");
    myPrefs.edit().clear(); 
    myPrefs.edit().commit();
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    finish();      
}

Please help me..

Thanks in advance

Piraba
  • 6,974
  • 17
  • 85
  • 135
  • possible duplicate of [Quitting an application - is that frowned upon?](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – CommonsWare Aug 24 '11 at 15:54

2 Answers2

1

Android applications don't have "exit" buttons. If you look at all of the applications that are part of the platform, none of them have exit buttons or menus.

If you want the user to return to the root activity of your app each time they launch it from its icon in the launcher, use this in your manifest:

http://developer.android.com/guide/topics/manifest/activity-element.html#clear

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • iN MY APPLICATION i HAVE "EXIT" button . When User click that one it need to go homescreen. That is ok. Calling back my app(Main Grid --> select my app) then its go to Last opens.That is the issue – Piraba Aug 24 '11 at 08:51
  • A lot of apps have Exit menu buttons. Especially useful for apps which run in the background by design, providing the user an easy way to end them. – CrackerJack9 Aug 24 '11 at 14:52
  • Actually, Google Analytics uses an Exit button in their sample App. – CrackerJack9 Aug 24 '11 at 17:13
  • @CrackerJack9 no this is mixing two very different things. If an app has something of interest it can do in the background, it makes fine sense to have a UI to control it. Starting/stopping music playback, navigation, being signed in to chat, etc. Again look at the standard platform. You will not see an exit button anywhere. You will certainly see things to control what an application does in the background, but this is not at all the same thing. And if Google Analytics has an actual exit button, then it is stupid, and don't copy it and be stupid as well. :p – hackbod Aug 26 '11 at 08:09
  • @hackbod seem to be hung up on a label. what's the difference betweeen a button that says 'Stop background service' and one that says 'Exit' if they both do the same thing? – CrackerJack9 Aug 26 '11 at 13:54
  • 2
    They definitely do not. Pretty universally when people ask about how to make an exit command, they are thinking about things as a desktop app, where they "exit" and all of the windows go away, the process goes away, etc. This is not a model that is appropriate for Android. A button that lets you stop a background service only impacts that background service -- if it is implemented as a Service, then that Service is destroyed, but your application's activities remain as well as its process. Or this may just remove an alarm manager alarm. Either way, very different from quit the app. – hackbod Aug 28 '11 at 02:28
-2
public void quit() {
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
        System.exit(0);
    }

and on exit button just call this method

btnExit.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        try {
                        quit(); 
                        } catch (Exception e) {

                        }
                    }
                });
Android
  • 8,995
  • 9
  • 67
  • 108