Kotlin-如何设置共享首选项代码,以便我打开已关闭的应用程序,它会打开上一个活动(我将其留在此处)

时间:2019-01-08 12:40:28

标签: kotlin sharedpreferences

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

1 个答案:

答案 0 :(得分:0)

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

在此SplashActivity中,您可以设置var lastActivity,这将是用于保留您上一次活动的代码。 您可以通过SharedPreference来获取它,然后转到该活动。

即:

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>