Android:ON・OFF設定のSwitchとSwichのデータ保持の為にsharedPreferencesを使用したサンプルアプリ実装
作成したアプリ
- Switchを使ったON・OFF切り替えアプリ。ON・OFF切り替え時SwitchとSwitchの文言が変更。アプリキル->再起動時ON・OFFの状態をsharedpreferencesで復元するアプリ
使用した機能
- SharedPreferences
-> アプリ内にデータを保持しておく仕組み。アプリキル後も.xmlにデータが保持される。
↓ sharedPreferencesの扱い方の例
// context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)で生成
// 第一引数に用いる名前でsharedPreferencesインスタンスを生成・取得する
sharedPreferences = view.context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
// sharedPreferencesの値をゲットする場合
// 保存した名前で値を取得。値が見つからない場合は第2引数のdefValueの値を取得
val isSwitchEnabled = sharedPreferences?.getBoolean(IS_ENABLED_SWITCH, true)
// sharedPreferencesに値をセットする場合
// edit()で編集開始 -> putメソッドでsharedPreferencesにデータ保存 -> applyで変更を保存
val editor = sharedPreferences?.edit()
if (isSwitchEnabled != null) {
editor?.putBoolean(IS_ENABLED_SWITCH, isSwitchEnabled)
}
editor?.apply()
- Switch
-> Buttonを継承しており、ON・OFFをタップとスライドで切り替えられるView。text()ではSwitchのボタンの横に自動で文言が追加でき、isCheckedにtrueかfalseをセットする事によりSwitchのON・OFFを設定できる。SwitchのON ・OFFの切り替え時に処理を切り替えたい場合は,CompoundButton.OnCheckedChangeListenerを実装しSwitchのsetOnCheckedChangeListenerにセットする。これによりonCheckedChange()はSwichが切り替わったタイミングで呼ばれるようになる。
今回実装した主要クラスのコードを展開しておく
package com.example.samplesharedpreferencesapplication
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.Switch
import androidx.fragment.app.Fragment
class MainFragment : Fragment(), CompoundButton.OnCheckedChangeListener {
companion object {
private const val FILE_NAME = "fileName"
private const val IS_ENABLED_SWITCH = "isEnabledSwitch"
}
private var sharedPreferences: SharedPreferences? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
@SuppressLint("UseSwitchCompatOrMaterialCode")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sharedPreferences = view.context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
val sampleSwitch = view.findViewById<Switch>(R.id.sampleSwitch)
val isSwitchEnabled = sharedPreferences?.getBoolean(IS_ENABLED_SWITCH, true)
Log.d("sharedPreferences", "$sharedPreferences")
isSwitchEnabled?.let { setSampleSwitch(it, sampleSwitch) }
}
private fun setSampleSwitch(isSwitchEnabled: Boolean, sampleSwitch: Switch) {
sampleSwitch.setOnCheckedChangeListener(null)
sampleSwitch.isChecked = isSwitchEnabled
sampleSwitch.setOnCheckedChangeListener(this)
sampleSwitch.text =
context?.getString(R.string.switch_text1) +
isSwitchEnabled.toString() +
context?.getString(R.string.switch_text2)
val editor = sharedPreferences?.edit()
editor?.putBoolean(IS_ENABLED_SWITCH, isSwitchEnabled)
editor?.apply()
}
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
setSampleSwitch(isChecked, buttonView as Switch)
}
}