How can I execute something just once per application start?

前端 未结 10 1349
感情败类
感情败类 2020-11-28 06:45

I\'d like to implement an update checker in an application, and I obviously only need this to show up once when you start the application. If I do the call in the onCr

10条回答
  •  有刺的猬
    2020-11-28 07:09

    This continues on @Vitalii's answer.

    After having setup the Application class, if access to the Activity is required, we can use the aptly named android library "Once" https://github.com/jonfinerty/Once.

    In the Application class's onCreate method

    Once.initialise(this)
    

    In the Activity / Fragment class's onCreate / onViewCreated method.

    val helloTag = "hello"
    if (!Once.beenDone(Once.THIS_APP_SESSION, helloTag)) {
        //Do something that needs to be done only once
        Once.markDone(helloTag) //Mark it done
    }
    

提交回复
热议问题