Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

android studio - How to get the data stored in a SharedPreference from a Fragment in another (Kotlin)

I've tried many other solutions but none have worked for me, I do not know what's wrong with my code. I'm a beginner and any answer is welcome, thanks. First fragment:

class settingsFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val binding: FragmentSettingsBinding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_settings, container, false)

    fun onSave() {
        val pref:SharedPreferences? = activity?.getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
        val editor: SharedPreferences.Editor? = pref?.edit()
        editor?.putString("STATE_ONE", "true")
        editor?.apply()
        Toast.makeText(context,"Settings Saved",Toast.LENGTH_LONG).show()
    }

    binding.applyButton.setOnClickListener {
        onSave()
        view?.findNavController()?.navigate(R.id.action_settingsFragment_to_startFragment)
    }
    return binding.root
  }
}

Second fragment:

    class startFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View {
    val binding: FragmentStartBinding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_start, container, false)

    binding.btnStart.setOnClickListener { view: View ->
        val preferences:SharedPreferences? = activity?.getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
        val state1 = preferences?.getString("STATE_ONE", "")
        Toast.makeText(context, state1, Toast.LENGTH_LONG).show()
    }
    binding.btnSettings.setOnClickListener { view: View ->
        view.findNavController().navigate(R.id.action_startFragment_to_settingsFragment)
    }
    return binding.root
  }
}
question from:https://stackoverflow.com/questions/65887916/how-to-get-the-data-stored-in-a-sharedpreference-from-a-fragment-in-another-kot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...