Take the 2-minute tour ×

In my app i have a log-in and logout mechanism. I want to clear the task stack when the user presses the logout button so that when he launches the application again he would again have to login. I looked up online and in most cases people were using this to accomplish it:

Intent i = new Intent(Intent.ACTION_MAIN);

with this code this happens:

home->login activity(main launcher activity)[uername and password fields are blank]->on clicking logout->home and then when i do this:

from home->launch app(in the login screen the username and password are still present)->navigate to home by pressing back button ->launch app again->username and password cleared

what would be a better way to write the logout functionality?

share|improve this question
i am currently doing that by setting the edit text as null in the onResume() function of the main activity. Its working fine but any other solutions are most welcome! :) –  Ash Apr 23 '12 at 3:18

2 Answers 2

up vote 0 down vote accepted

You can save username and password in shared preferences database.and when user logouts then clear values from database in same key.

if user directly exits app without logout check if username and password already exists you can show them into edittext.

to save in database

    SharedPreferences settings = getSharedPreferences("DB_NAME", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("user", username);
    editor.putString("pass", password);

to clear from database

    SharedPreferences settings = getSharedPreferences("DB_NAME", 0);
    SharedPreferences.Editor editor = settings.edit();
share|improve this answer
i dint want to do that because i already saved much info related to users in shared preferences. anyways thanks for your answer! :) –  Ash Apr 23 '12 at 5:56

Override the Application class and create a public field (or private with getter/setter).

In application class:

public boolean loginDialogShown = false;

In login dialog code:

MyApplication.loginDialogShown = true;

In acivity:

if (!MyApplication.loginDialogShown){ loginDialog.show(); }


share|improve this answer

Your Answer


