Kotlin - 我如何设置共享首选项代码,以便我打开关闭的应用程序,它会打开最后一个活动,我离开它的地方

阿布迪

我在我的应用程序中做了两个活动,我希望应用程序在我离开的地方打开。换句话说,不是默认活动,而是我上次退出应用程序时所在的活动。

敌人

您可以设置一个 SplashActivity,在您的应用程序启动的地方,它将启动其他活动。

在这个 SplashActivity 中,你可以设置一个 var lastActivity,这将是一个代码来保存你上次在哪个活动中。您可以使用 SharedPreference 获取它,然后转到活动。

IE :

String lastActivity = SharedPreference.getString(...) // I don't really remember the syntax
if (lastActivity == "HelloWorldActivity")
    startActivity(HelloWorldActivity.getStartIntent(context))
else if (lastActivity == "GoodByeActivity")
    startActivity(GoodByeActivity.getStartIntent(context))

然后,不要忘记在每次更改活动时编辑您的 SharedPreference 值。

我不知道这是否是一个好的做法,但请随意测试并给出您的想法。

编辑

首先,您需要了解如何共享首选项文件。我认为它看起来像这样:

"app_name"="Your app name"
"last_activity"="Your last activity"
"user_age"="23"

这可能是您启动的第一个活动:

class SplashActivity : AppCompatActivity() {

    var lastActivity = ""

    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate()

        /*
            Here, we will get the SharedPreferencesFile.
            Then, we get the value linked to the key TAG_LAST_ACTIVITY (set in companion object)
        */
        val sharedPref = this.getSharedPreferences(getString(R.string.shared_preference_file_name), 0)
        lastActivity = sharedPref.getString(TAG_LAST_ACTIVITY, "")

        var activityToStart : AppCompatActivity? = null
        if (lastActivity.isBlank())
             activityToStart = YourActivityToStartAtFirstLaunch.getStartIntent(this)
        else if (lastActivity.equals(TAG_ACTIVITY_ONE))
             activityToStart = ActivityOne.getStartIntent(this)
        else if (lastActivity.equals(TAG_ACTIVITY_TWO))
             activityToStart = ActivityTwo.getStartIntent(this)
        else if
             ... // Use as many as else if you need, but think about the "when" condition, it is better !

        startActivity(activityToStart)
    }

    companion object {
        private const val TAG_LAST_ACTIVITY = "last_activity"
        private const val TAG_ACTIVITY_ONE = "activity_one"
        private const val TAG_ACTIVITY_TWO = "activity_two"
    }
}

这可能是您的 ActivityOne,例如:

class ActivityOne : AppCompatActivity() {

    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate()

        /*
            Here, we will modify the variable LAST_ACTIVITY in the shared preferences file by setting it to "activity_one".
            So, if the user quit this app now, you will know at next launch in which activity he stopped.
            I think it is a better practice to set this in the onPause() or onStopped() method. Think about it ! ;)
        */
        val sharedPrefEditor = this.getSharedPreferences(getString(R.string.shared_preference_file_name, 0)).edit()
        sharedPrefEditor.putString(TAG_LAST_ACTIVITY, TAG_ACTIVITY_ONE)
        sharedPrefEditor.apply()
    }

    companion object {
        fun getStartIntent(context : Context) : Intent = Intent(context, ActivityOne()::class.java)

        private const val TAG_ACTIVITY_ONE = "activity_one"
        private const val TAG_LAST_ACTIVITY = "last_activity"
    }    
}

不要忘记将共享首选项文件名放在 values/strings.xml 文件中:

<string name="shared_preference_file_name">com.example.yourappname.sharedpref"</string>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档