fix NPE and Target fragment must implement TargetFragment interface issue

This commit is contained in:
Milos Kozak 2021-01-20 15:07:01 +01:00
parent 8722b64b03
commit d2517f0beb
2 changed files with 12 additions and 14 deletions

View file

@ -420,6 +420,6 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang
fun setFilter(filter: String) { fun setFilter(filter: String) {
this.filter = filter this.filter = filter
updateFilterVisibility(filter, preferenceScreen) preferenceManager?.preferenceScreen?.let { updateFilterVisibility(filter, it) }
} }
} }

View file

@ -33,23 +33,21 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa
supportActionBar?.setDisplayShowHomeEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true)
myPreferenceFragment = MyPreferenceFragment() myPreferenceFragment = MyPreferenceFragment()
preferenceId = intent.getIntExtra("id", -1) preferenceId = intent.getIntExtra("id", -1)
val args = Bundle() myPreferenceFragment?.arguments = Bundle().also {
args.putInt("id", preferenceId) it.putInt("id", preferenceId)
args.putString("filter", pref_filter.text.toString()) it.putString("filter", pref_filter.text.toString())
myPreferenceFragment?.arguments = args }
if (savedInstanceState == null)
supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit() supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit()
} }
override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean { override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean {
val fragment = MyPreferenceFragment() val fragment = MyPreferenceFragment()
val args = Bundle() fragment.arguments = Bundle().also {
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.key) it.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.key)
args.putInt("id", preferenceId) it.putInt("id", preferenceId)
fragment.arguments = args }
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction().replace(R.id.frame_layout, fragment, pref.key).addToBackStack(pref.key).commit()
.replace(R.id.frame_layout, fragment, pref.key)
.addToBackStack(pref.key)
.commit()
return true return true
} }