简体   繁体   English

Android Studio 注销:清除保存的首选项 kotlin

[英]Android studio logout: clear saved preferences kotlin

I have one main activity with a login button, and I keep a token when a user logs in (saved preferences).我有一个带有登录按钮的主要活动,当用户登录时我会保留一个令牌(保存的首选项)。 If the user is logged in, the app goes straight to my second activity, which is SearchActivity.如果用户已登录,应用程序会直接进入我的第二个活动,即 SearchActivity。 I have added a logout button there, but I cant seem to make it work.我在那里添加了一个注销按钮,但我似乎无法让它工作。 I want it to release the saved token.我希望它释放保存的令牌。 I'm new to Kotlin and Android, so any help would be really useful.我是 Kotlin 和 Android 的新手,所以任何帮助都会非常有用。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        login_button.setOnClickListener {
            buttonClicked()
        }

        if (getTokenFromPrefs()?.length!! > 0) {
            openSearchActivity()
        }
    }

    private fun buttonClicked() {
        val username = username_edittext.text.toString()
        val password = password_edittext.text.toString()

        val call = RequestManager.service.login(username, password)
        call.enqueue(object : Callback<List<LoginResponse>> {
            override fun onResponse(
                call: Call<List<LoginResponse>>,
                response: Response<List<LoginResponse>>
            ) {
                loader.visibility = View.GONE
                if (response.isSuccessful) {
                    openSearchActivity()
                    saveTokenToPrefs("token")
                } else {

                }
            }

            override fun onFailure(call: Call<List<LoginResponse>>, t: Throwable) {
                loader.visibility = View.GONE
            }
        })
    }

    val TOKENPREFSKEY = "tokenprefskey"
    private fun saveTokenToPrefs(token: String) {
        val pref = applicationContext.getSharedPreferences("CGEEnergy", 0)
        val editor = pref.edit()
        editor.putString(TOKENPREFSKEY, token)
        editor.commit()
    }

    private fun getTokenFromPrefs(): String? {
        val pref = applicationContext.getSharedPreferences("CGEEnergy", 0)
        return pref.getString(TOKENPREFSKEY, "")
    }

    private fun openSearchActivity() {
        val intent = Intent(this, SearchActivity::class.java)
        startActivity(intent)
        finish()
    }

My search activity class is this (kind of empty for now):我的搜索活动类是这样的(现在有点空):

class SearchActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)


    }

    override fun onResume() {
        super.onResume()
    }
}

也许试试这个:

applicationContext.getSharedPreferences("CGEEnergy", 0).edit().clear().commit()

删除您可以使用的共享首选项

context.deleteSharedPreferences("CGEEnergy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM