简体   繁体   English

在 Android 和 session 中存储公共数据的最佳方式?

[英]Best way to store common data in Android at session?

When user open my Application.当用户打开我的应用程序时。

It will call API to my server, and take the json and stored in somewhere, and the json It stored can use anywhere in my application (any Activity , any Fragment , like SharedPreference ).它会调用 API 到我的服务器,并将json存储在某个地方,而它存储的json可以在我的应用程序中的任何地方使用(任何Activity ,任何Fragment ,如SharedPreference )。

When user close the application, data will clear.当用户关闭应用程序时,数据将被清除。 Reopen it, It's will calling and storing data again.重新打开它,它会再次调用和存储数据。

What method should I can use?我应该使用什么方法? Or any other implementation?或者任何其他实现? Thank you谢谢

Extend Application class like this:像这样扩展应用程序 class:

    class Global: Application() {
    
        init { instance = this }
    
        companion object {
            private var instance: Global? = null
            lateinit var json: JSONProp
        }

        override fun onCreate() {
            super.onCreate()
            //Fetch Json and store it in jsonProp
        }
    }

Manifest:显现:

<application android:name=".Global" />

You can then use the data anywhere in the app while it's alive like this:然后,您可以在应用程序中的任何位置使用数据,就像这样:

val json = Global.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM