与 Kotlin 在 Fragment 中共享首选项
Shared Preferences in Fragment with Kotlin
我正在 Android 使用 Kotlin 制作一个计数器应用程序。我的代码在 MainActivity 中运行良好,但当涉及到片段时,它就不再工作了。
class HomeFragment : Fragment()
{
private lateinit var homeViewModel: HomeViewModel
@SuppressLint("SetTextI18n")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
{
/*homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)*/
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
val button: Button = root.findViewById<Button>(R.id.button)
var nombre = PrefConfing.loadTotalFromPref(this)
button.setOnClickListener {
nombre++
textView.text = "vous l'avez fait $nombre fois"
PrefConfing.saveTotalTimes(applicationContext, nombre)
}
return root
}
}
这是我的 Kotlin HomeFragment 代码,还有我的 Java 代码:
public class PrefConfing {
private static final String TIMES = "com.example.alllerrr";
private static final String PREF_TOTAL_KEY = "pref_total_key";
public static void saveTotalTimes(Context context, int total) {
SharedPreferences pref = context.getSharedPreferences(TIMES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt(PREF_TOTAL_KEY, total);
editor.apply();
}
public static int loadTotalFromPref(Context context){
SharedPreferences pref = context.getSharedPreferences(TIMES, Context.MODE_PRIVATE);
return pref.getInt(PREF_TOTAL_KEY, 0);
}
}
对于 var nombre
我无法将其添加到上下文中,我不明白为什么。
如果在行
var nombre = PrefConfing.loadTotalFromPref(this)
您将获得:
Type mismatch.
Required: Context!
Found: HomeFragment
你必须这样做:
var nombre = PrefConfing.loadTotalFromPref(requireContext())
你会知道为什么的:
如果你追寻 Activity class 的继承,你会发现它继承了 Context class 所以传递 "this"没有问题,
但是当你追寻Fragmentclass继承(androidx.fragment.app.Fragment)的时候,你永远不会发现class继承了Context,所以不接受将 "this" 作为 Context.
实际上 getContext 和 requireContext returns 它们的主机上下文,因此您需要改用它们。
requireContext: returns a nonnull Context, or throws an exception when one isn't available.
getContext: returns a nullable Context.
更多关于“getContext”和“requireContext”的区别。
我正在 Android 使用 Kotlin 制作一个计数器应用程序。我的代码在 MainActivity 中运行良好,但当涉及到片段时,它就不再工作了。
class HomeFragment : Fragment()
{
private lateinit var homeViewModel: HomeViewModel
@SuppressLint("SetTextI18n")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
{
/*homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)*/
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
val button: Button = root.findViewById<Button>(R.id.button)
var nombre = PrefConfing.loadTotalFromPref(this)
button.setOnClickListener {
nombre++
textView.text = "vous l'avez fait $nombre fois"
PrefConfing.saveTotalTimes(applicationContext, nombre)
}
return root
}
}
这是我的 Kotlin HomeFragment 代码,还有我的 Java 代码:
public class PrefConfing {
private static final String TIMES = "com.example.alllerrr";
private static final String PREF_TOTAL_KEY = "pref_total_key";
public static void saveTotalTimes(Context context, int total) {
SharedPreferences pref = context.getSharedPreferences(TIMES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt(PREF_TOTAL_KEY, total);
editor.apply();
}
public static int loadTotalFromPref(Context context){
SharedPreferences pref = context.getSharedPreferences(TIMES, Context.MODE_PRIVATE);
return pref.getInt(PREF_TOTAL_KEY, 0);
}
}
对于 var nombre
我无法将其添加到上下文中,我不明白为什么。
如果在行
var nombre = PrefConfing.loadTotalFromPref(this)
您将获得:
Type mismatch.
Required: Context!
Found: HomeFragment
你必须这样做:
var nombre = PrefConfing.loadTotalFromPref(requireContext())
你会知道为什么的:
如果你追寻 Activity class 的继承,你会发现它继承了 Context class 所以传递 "this"没有问题, 但是当你追寻Fragmentclass继承(androidx.fragment.app.Fragment)的时候,你永远不会发现class继承了Context,所以不接受将 "this" 作为 Context.
实际上 getContext 和 requireContext returns 它们的主机上下文,因此您需要改用它们。
requireContext: returns a nonnull Context, or throws an exception when one isn't available.
getContext: returns a nullable Context.