How can I collect data in one fragment and show it in another fragment in android studio, I am using Shared Preferences to do this

Viewed 94

This is the function I made in one fragment to store my value,

  private fun nickdata(){
    

    val sharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
    val nicdat = binding.nickname.text.toString()
    val editor = sharedPreferences.edit()
    
    editor.putString("Nickname_key","trial")
    editor.apply()
    editor.commit()}

below is the code I am using to fetch data in another fragment

    val pref = activity!!.getPreferences(Context.MODE_PRIVATE)
    val id = pref.getString("Nickname_Key", "trial")
    binding.nickdata.text = id

In output I am getting "trial" which is def value.

3 Answers

Make sure you are using the same shared preference in both fragments.

Also what you did is you stored the value trial not the value that you want to send the the other fragment.

val sharedPreferences = activity!!.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
val nicdat = binding.nickname.text.toString()
val editor = sharedPreferences.edit()

editor.putString("Nickname_key",nicdat)
editor.apply()
editor.commit()

For the other fragment

val pref = activity!!.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
val id = pref.getString("Nickname_Key", "trial")
binding.nickdata.text = id

You can use Fragment Result Listeners. More documentation: Communicating between fragments. Here is some working code that controls navigation in my app:

In first fragment:

open fun navigate(idOfDestination: Int, bundle: Bundle?) {
    val tempBundle = bundle ?: Bundle()
    tempBundle.putInt(NAVIGATION_DESTINATION, idOfDestination)
    activity?.supportFragmentManager?.setFragmentResult(NAVIGATION_RESULT, tempBundle)
}

In target fragment, register the listener:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    activity?.supportFragmentManager?.setFragmentResultListener(NAVIGATION_RESULT, this, FragmentResultListener { _, bundle ->
        val destination = bundle.getInt(NAVIGATION_DESTINATION)
        bundle.remove(NAVIGATION_DESTINATION)
        navigationHandler(bundle,destination)
    })
}

private fun navigationHandler(bundle: Bundle?, idOfDestination: Int) {
    navController = Navigation.findNavController(binding.root)
    if (!FeatureControlManager.isDestinationAllowedToGo(idOfDestination)) {
        navController = childFragmentManager.fragments.first().findNavController()
    }
    navController.validateAndNavigate(navController, idOfDestination, childFragmentManager, bundle)
}

I got it done

 private fun nickdata(){
    val sharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
    val nicdat = binding.nickname.text.toString()
    val editor = sharedPreferences.edit()

    editor.putString("trial",nicdat)
    editor.apply()
    editor.commit()

this code in one fragment below mentioned code where I want to fetch it

    val pref = activity!!.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
    val nicdat = binding.nickdata.text.toString()
    val id = pref.getString("trial", nicdat)
    binding.nickdata.text = id
Related