Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当用户直接关闭应用程序时,在何处清理SharedPreference?_Android_Sharedpreferences - Fatal编程技术网

Android 当用户直接关闭应用程序时,在何处清理SharedPreference?

Android 当用户直接关闭应用程序时,在何处清理SharedPreference?,android,sharedpreferences,Android,Sharedpreferences,我不熟悉android和java。我尝试共享引用来管理会话。一旦我登录到屏幕,我就会写下面的代码 SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("login", true); editor.commit(); 当我关闭应用程序并再次启动时,它直接进入主屏幕,而不询问

我不熟悉android和java。我尝试共享引用来管理会话。一旦我登录到屏幕,我就会写下面的代码

SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("login", true);
editor.commit(); 

当我关闭应用程序并再次启动时,它直接进入主屏幕,而不询问凭据。 我不知道在哪里写以下几行:

SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
我想清除SharedReferences,即使应用程序直接关闭。我试过用finalize,但没用

编辑

我将下面的代码添加到我的文件中。这是工作,但给错误

代码:

错误:

android.app.SuperNotCalledException: Activity {com.ec.testtab/com.ec.testtab.Tabs} did not call through to super.onStop()

我已经在这两种活动方法中处理了这些事情,它们在您的情况下也会被调用,所以请尝试一下:

@Override
protected void onResume() {
    super.onResume();
    // Start Logging
}

@Override
protected void onPause() {
    super.onPause();
    // End Logging
}

最好在应用程序关闭时将
prefs
设置为false。当您再次返回时,只需检查此prefstruefalse

false---表示不登录

真的----意味着登录


如果您不想在应用程序关闭时存储共享首选项,则应在每次启动应用程序时清除共享首选项,这样,在执行应用程序的进一步处理时,不会出现以前的共享首选项。

一旦用户使用有效凭据登录到应用程序,则仅将共享首选项登录值更改为true

  SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
   SharedPreferences.Editor editor = prefs.edit();
   editor.putBoolean("login", true);
  editor.commit(); 

每次在Oncreate上只需检查“login”值即可确定用户是否已经登录

SharedReferences用于持久性存储,如果您不需要为下一个启动器存储信息,请使用
公共静态布尔isLogin
可能是一种简单的方法:

public static boolean sIsLogin = false;
...
if(doLogin()){
sIsLogin = true;
}
...

当你关闭应用程序并再次启动时,
sIsLogin
为false。

关闭意味着什么?使用back按钮?@Nabin,不,按住home按钮一秒钟,将应用刷到一边,“它直接进入主屏幕而不询问凭据”。你到底想要什么?如果用户登录一次,那么如果您再次启动应用程序,您不想登录屏幕?如果我销毁应用程序并再次启动,它必须询问username和pswd@PG_androidol。一年后,我得到了-1。我的问题是:我杀死了这个应用程序。它不再跑了。我又开始了。然后它就可以不经请求直接登录了password@SGG仔细阅读我的答案。。。你要做的事情..当你的应用程序关闭时(意味着在活动级别或应用程序级别),只需将prefs值设置为false,当你再次返回时,检查此prefs值..如果值为false,则加载密码屏幕,如果不是,则直接进入主屏幕..你的意思是,在onDetsroy方法中?@sg add
super.onStop()
我想清理onDestroy的首选项。您直接到达主屏幕意味着onDestroy未被调用查看活动生命周期添加super.onStop();对于您的onStop方法,这就是logcat所说的,您可以在启动程序活动的onCreate()方法中尽快清除它。
You can add this lines of code

 SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("login", false);
            editor.commit(); 

when your app starts before fetching the username and password from the preferences. So that every time  you start the app it will show login page.
You can add this lines of code

 SharedPreferences prefs = getSharedPreferences("myPref", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("login", false);
            editor.commit(); 

when your app starts before fetching the username and password from the preferences. So that every time  you start the app it will show login page.