Is the static safe in Android?

后端 未结 2 1133
故里飘歌
故里飘歌 2021-02-06 07:31

I use a single static class in my code that defines a static field which I\'m reusing between Activity onStop/onStart invocations. Here\'s a scenario:

  1. User clicks
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 08:13

    That is not safe. Your process can be killed between onStop and onStart, so all static data will be gone. In fact your activity can even be killed before it gets to onStop. In your tests the process was not killed, but it was for the user. See the Android activity life cycle which has a nice flow chart showing the possibilities.

    You need to store the data some other way, in prefs or database for example.

提交回复
热议问题