开发者

static variables keep values from previous launch

开发者 https://www.devze.com 2023-02-15 19:45 出处:网络
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 prev

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 prev开发者_StackOverflow中文版ious run.

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


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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消