Kotlin:我如何检查该应用程序是否已经打开过一次?
Kotlin: how do i check if the app has already been opened once?
我正在 Flutter 中构建一个应用程序,需要 运行 Kotlin 中的一段代码来检查该应用程序是否是第一个 运行。
我发现这是 Java 中的代码,但我该如何在 Kotlin 中实现它?
public class MyActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
}
}
}
其实很简单:
class MainActivity : Activity() {
private lateinit var sharedPrefs: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPrefs = getSharedPreferences(packageName, MODE_PRIVATE)
}
override fun onResume() {
super.onResume()
if (sharedPrefs.getBoolean("firstRun", true)){
sharedPrefs.edit().putBoolean("firstrun", false).commit()
}
}
}
我正在 Flutter 中构建一个应用程序,需要 运行 Kotlin 中的一段代码来检查该应用程序是否是第一个 运行。
我发现这是 Java 中的代码,但我该如何在 Kotlin 中实现它?
public class MyActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
}
}
}
其实很简单:
class MainActivity : Activity() {
private lateinit var sharedPrefs: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPrefs = getSharedPreferences(packageName, MODE_PRIVATE)
}
override fun onResume() {
super.onResume()
if (sharedPrefs.getBoolean("firstRun", true)){
sharedPrefs.edit().putBoolean("firstrun", false).commit()
}
}
}