static variables keep values from previous launch

五迷三道 提交于 2020-01-25 04:19:12

问题


My j2me application call destroyApp() and notifyDestroyed() when it wants to be closed when I run it on blackberry, in the second launch the static variables will have the same values they had in previous run.

Why they don't get the initial value? How can I make sure the application initialize static variables?


回答1:


This discussion at Blackberry support forums suggests that it is a known issue with BlackBerry MIDlets - at least with those using static push registries. Workarounds they suggest are either re-define static variables in startApp or get rid of static push.

This post looks worth extensive quoting, since there is a nice code example and issue analysis:

The simplest example I could come up with is this:

public class BasicMIDlet extends MIDlet {

    private static byte myByte = Byte.MIN_VALUE;

    public void startApp() {
        System.out.println("My byte: " + myByte);
        myByte = Byte.MAX_VALUE;
        notifyDestroyed();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }


}

You would expect myByte to output to -128 every time the app starts, but this is my output:

--------------------------------------------------------

Starting BBTest
Started BBTest(159)
Foreground BBTest(157)
My byte: -128                          <------------
Foreground net_rim_bb_ribbon_app(83)
Exit BBTest(159)
Starting BBTest
Started BBTest(160)
Foreground BBTest(157)
My byte: 127                          <------------
Foreground net_rim_bb_ribbon_app(83)
Exit BBTest(160)
Starting BBTest
Started BBTest(161)
Foreground BBTest(157)
My byte: 127                          <------------
Foreground net_rim_bb_ribbon_app(83)
Exit BBTest(161)
--------------------------------------------------------

I have noticed something, if I remove the static push registries, the application behaves normally and outputs -128 every time. An yes, I have the same feeling that a MIDlet runs on top of a RIMlet, and in the case the midlet defines push registries, the RIMlet is running all the time. So my question now is, are there any solutions other than initializing the static variables on every run (because there are roughly >1000 such members in my app)




回答2:


Yep, blackberry midlets retain the values of static variables. This is an issue, and the only way I see to fix it is that in the startup we need to assign null values to the static values. For example if a static var is declared like: public static String State = null; And in the life cycle of the middle the value is set to "closed"; Then in the next startup of the application, the value remains "closed" instead of null.




回答3:


I guess you mean "in the second launch the static variables will not have the same values they had in previous run".

Static variables can only maintain their value through the lifetime of the app. The app ends when destroyApp()/notifyDestroyed() is called, so the values are lost!

To persist state over multiple runs, use the RecordStore.



来源:https://stackoverflow.com/questions/5258818/static-variables-keep-values-from-previous-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!