【问题标题】:Android destroy session when user closes the application用户关闭应用程序时Android销毁会话
【发布时间】:2014-10-23 06:21:59
【问题描述】:

我正在开发一个 android 应用程序,其中首先是登录页面,从登录重定向后有一个菜单页面。如果我在任何其他页面上并且我关闭了我的应用程序,我想销毁我的会话,当我打开我的应用程序时,应该打开登录页面。从任何其他页面,如果我按下设备的后退按钮,那么我只想重定向到菜单页面。有人可以帮帮我吗?

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

  super.onResume();
}

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

我在每个页面中都使用了此代码,但这不起作用..

当我按下 HOME 按钮时,destroy() 不会被调用。但我希望当我按下 HOME 按钮时,所有活动都应该被销毁。

【问题讨论】:

    标签: android android-activity android-authenticator


    【解决方案1】:

    你可以。销毁活动中的会话/cookie/SharedPreferences。

    在Activity中做这个过程...调用这些方法:

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

    或者

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

    或者您可以在 Back Key 事件中执行此操作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2013-06-17
      • 1970-01-01
      相关资源
      最近更新 更多