You can use sharedPreferences like this instead of call editor from SharedPreferences
class SettingFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding by lazy { FragmentSettingBinding.inflate(inflater, container, false) }
fun onSave() {
val pref = requireActivity().getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
pref?.edit { putString("STATE_ONE", "true") }
Toast.makeText(context, "Settings Saved", Toast.LENGTH_LONG).show()
findNavController().navigate(R.id.action_settingFragment_to_startFragment)
}
binding.btnApply.setOnClickListener { onSave() }
return binding.root
}
}
class StartFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding by lazy { FragmentStartBinding.inflate(inflater, container, false) }
binding.btnStart.setOnClickListener {
val pref = requireActivity().getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
val state = pref?.getString("STATE_ONE", "")
Toast.makeText(context, state, Toast.LENGTH_LONG).show()
}
return binding.root
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…