C-BK:
私は、ログインボタンと一つのメインの活動を持っている、と私はトークンを保持したときにユーザーがログイン(保存設定)。ユーザーがログインしている場合、アプリはSearchActivityある私の第二の活動に直進します。私はそこにログアウトボタンを追加したが、私はそれを動作させるように見える傾けます。私はそれが保存されたトークンをリリースしたいです。すべてのヘルプは本当に便利になるので、私は、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()
}
マイ検索アクティビティクラスは、この(今の空のようなもの)です。
class SearchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
}
override fun onResume() {
super.onResume()
}
}
Littlish:
多分これを試してみてください。
applicationContext.getSharedPreferences("CGEEnergy", 0).edit().clear().commit()