是静态安全的机器人?静态、机器人、安全

2023-09-05 00:51:35 作者:流年无恙

我在code,它定义了一个静态字段,我活动的onStop / ONSTART调用之间重用使用一个静态类。这里有一个场景:

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:

在用户点击授权按钮(静态数据初始化) 在活动停止,网页浏览器被称为 在浏览器中执行回调活性得以恢复(静态数据重复使用)

至少有一个我的用户报告故障,在第3步,我无法重现,但它看起来像的静态数据重置

At least one of my users reports the failure at step 3 which I cannot reproduce but which looks like reset of static data

有什么建议?

推荐答案

这是不安全的。你的过程的onStop和ONSTART之间被杀死,因此,所有静态数据将不复存在。事实上之前它到达的on​​Stop你的活动,甚至可以杀死。在您的测试过程中没有被打死,但它的用户。请参阅的Andr​​oid活动的生命周期它有一个很好的流程图示出准备。

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.

您需要存储数据的其他方式,在preFS或数据库为例。

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