I change the darkTheme as follows in the SettingsFragment under MainActivity and MainActivity is recreated normally. However, when I return to the MainFragment later, all the methods in the TextInputEditText are called meaninglessly. How can I prevent this?
SettingsFragment
var sharedPreferences = requireActivity().getSharedPreferences("Mode", Context.MODE_PRIVATE)
var isNightMode = sharedPreferences.getBoolean("nightmode",false)
with(binding){
if (isNightMode)
darkThemeSwitch.isChecked = true
darkThemeSwitch.setOnCheckedChangeListener { compoundButton, b ->
if (b){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
sharedPreferences.edit().putBoolean("nightmode",true).apply()
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
sharedPreferences.edit().putBoolean("nightmode",false).apply()
}
}
}
The TextInputEditText I meant above is written as searchBarEditText below.
MainFragment
class MainFragment : Fragment(){
private var _binding : FragmentMainBinding? = null
private val binding get() = _binding!!
private val placeList = arrayListOf<Place>()
private var placesAdapter : PlacesAdapter? = null
private val TAG = "TRVL_MainFragment"
private lateinit var db : FirebaseFirestore
private var kullaniciAdi = ""
private var email = ""
private var userUid = ""
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentMainBinding.inflate(inflater,container,false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.i(TAG,"onViewCreated")
initializeFirebaseTools()
createRecyclerView()
settings()
getPlaces()
}
private fun settings() {
val settingsButton = headerView.findViewById<ImageView>(R.id.settingsButton)
settingsButton.setOnClickListener {
binding.drawerLayout.closeDrawer(GravityCompat.START)
val action = MainFragmentDirections.actionMainFragmentToSettingsFragment()
Navigation.findNavController(it).navigate(action)
}
}
private fun initializeFirebaseTools() {
db = FirebaseFirestore.getInstance()
userUid = mAuth.currentUser?.uid.toString()
}
private fun createRecyclerView() {
Log.i(TAG,"createRecyclerView and Adapter")
placesAdapter = PlacesAdapter(placeList)
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.recyclerView.adapter = placesAdapter
}
private fun getPlaces() {
Log.i(TAG,"getPlaces")
var collectionTAG = ""
when(localeCode){
"tr" -> collectionTAG = "Places"
"en" -> collectionTAG = "PlacesEng"
else -> collectionTAG = "Places"
}
db.collection(collectionTAG).addSnapshotListener { value, error ->
if (error != null){
Toast.makeText(requireContext(),"Error: " + error.localizedMessage,Toast.LENGTH_LONG).show()
} else {
if (value != null){
Log.i(TAG,"value is not null")
if (!value.isEmpty){
Log.i(TAG,"value is not Empty")
placeList.clear()
val documents = value.documents
for (document in documents){
val name = document.getString("name")
val desc = document.getString("desc")
val photos = document.get("photos") as ArrayList<String>?
val placeId = document.getString("placeId")
val productId = document.getString("productId")
if (name != null && desc != null && !photos.isNullOrEmpty() && placeId != null && productId != null){
val place = Place(name,desc,photos,placeId,productId)
placeList.add(place)
Log.i(TAG,"placeList -> " + placeList.size)
} else {
Log.e(TAG,"one of the parameters is null")
}
}
placesAdapter?.notifyDataSetChanged()
}
}
}
}
binding.searchBarEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
Log.i(TAG,"beforeTextChanged: " + charSequence.toString())
}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
Log.i(TAG,"onTextChanged: " + charSequence.toString())
}
override fun afterTextChanged(editable: Editable) {
val text = editable.toString()
Log.i(TAG,"afterTextChanged: " + text)
placesAdapter?.filter(text)
}
})
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}