androidandroid-activityandroid-authenticator

Android destroy session when user closes the application


I am developing an android application in which first is login page, after redirecting from login there is a menu page. If I am on any other page and I close my application, I want to destroy my session and when i open my application, the login page should be opened. From any other page if I press back button of my device then I want to redirect to menu page only. Can anybody help me?

@Override
protected void onResume() {
    if (not logedin)
    {
        logout;
        redirect to menu;
    } 

  super.onResume();
}

@Override
protected void onDestroy() {    
   logout;
   super.onDestroy();
}        

I used this code in every page but this is not worked..

When I pressed HOME button destroy() is not called.. But I want that when I pressed HOME button, all activities should be destroyed.


Solution

  • You can. Destroy your session/cookie/SharedPreferences in activity.

    Do this process in Activity... call these methods:

    onDestroy(){
        context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();
    }
    

    Or

    onResume(){
        context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();
    }
    

    Or you can do this on Back Key event:

     public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                // do your task
            }
            return super.onKeyDown(keyCode, event);
        }