-1

In my application when user clicks logout button, app takes him to login screen again and when he pressess back button from that logIn screen, toast appears that "u must login to continue.

I have done this to stop back navigation because if back navigation is possible then he would get all previous activities alive. After it when I press HOME button to exit from app, it minimizes the app and whenever I start my application, it starts from LogIn screen.

I need it to start it from splash screen .. what should I do ?
I have used this code in login screen to make back navigation impossible.

      @Override
 public void onBackPressed() {


    Toast.makeText(getApplicationContext(), "You Must login to continue!", Toast.LENGTH_LONG).show();

    }

Is it a bad idea to use "HOME BUTTON" to exit from application?

rene
  • 41,474
  • 78
  • 114
  • 152
Shanzay
  • 3
  • 2

1 Answers1

1

This is not how you should handle a "lougout" - you have many options but do not change the functionality of "BACK" ever.

You can consider using "startActivityForResult" and then have a result code for LOGOUT that is checked in each activity after "login" so you can control logout.

Also, you can have a logout screen that starts your task stack and goes straight to "login" if the user is not logged in or to a home page if they are. Then if "logout" is called, clear the task stack, put "logout" on top and it should finish and go to "login" only.

Something like this:

Intent logoutIntent = new Intent(c.get(), Authenticate.class);
logoutIntent.putExtra(AUTH_STATUS, false);
logoutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
      | Intent.FLAG_ACTIVITY_CLEAR_TOP);        
startActivity(logoutIntent);

Check your manifest to be a singleTask Activity:

    <activity   android:name=".Authenticate"
                android:launchMode="singleTask"
                android:label="@string/AuthenticateActName">
    </activity>

Then in authenticate make sure that your LOGIN variable reset and go to your authenticate screen.

There are other methods you might come up with, but those are two suggestions that have worked for me.

Jim
  • 10,172
  • 1
  • 27
  • 36
  • i used the method of clearing back stack whenever new activity was called but it stoped back navigation i mean when i clicked back button it took me out of application.. i dont need that i just wanted to destroy all activities on LOGOUT .. – Shanzay Jul 20 '14 at 23:34
  • can u explain ur anwer with the help of some code so that i can get it in a better way .. please :) – Shanzay Jul 20 '14 at 23:36