开发者

BlackBerry java detecting screen foreground event

开发者 https://www.devze.com 2023-03-15 10:05 出处:网络
In my BlackBerry application, I have a home screen.The user can then navigate to a settings screen.When the 开发者_开发知识库user goes back to the home screen, is there no method that is called on the

In my BlackBerry application, I have a home screen. The user can then navigate to a settings screen. When the 开发者_开发知识库user goes back to the home screen, is there no method that is called on the home screen indicating that the screen has come to the foreground?

I have tried onFocus() with no avail.

Thanks!


Unfortunately, hooking on the onExposed is not enough. I found that in Blackberry dialogs are also screens and even context menus are screens too. They are pushed on top of your screen so you receive onExposed callback when they are dismissed.

Though it's OK in many cases, in other cases it poses a problem - e.g. if I must refresh the screen's content only when the user returns to it, but not after menus/dialogs, then how do I do that? My case is, unfortunately, one of those.

I found no documented way of detecting "covered"/"uncovered" events. Here is my approach. onCovered/onUncovered callbacks are called when the current screen is covered/uncovered by another screen of the app, but not by dialogs/menus/virtual keyboard:

public class MyAppScreen extends MainScreen {
    private boolean isCovered;

    protected void onExposed() {
        Log.d("onExposed");
        super.onExposed();
        if (isCovered) {
            onUncovered();
            isCovered = false;
        }
    }

    protected void onObscured() {
        Log.d("onObscured");
        super.onObscured();
        final Screen above = getScreenAbove();
        if (above != null) {
            if (isMyAppScreen(above)) {
                isCovered = true;
                onCovered();
            }
        }
    }

    private boolean isMyAppScreen(final Screen above) {
        return (above instanceof MyAppScreen);
    }

    protected void onUncovered() {
        Log.d("onUncovered");
    }

    protected void onCovered() {
        Log.d("onCovered");
    }

    protected void onUiEngineAttached(final boolean attached) {
        if (attached) {
            Log.d("UI Engine ATTACHED");
        } else {
            Log.d("UI Engine DETACHED");
        }
        super.onUiEngineAttached(attached);
    }

    protected void onFocusNotify(final boolean focus) {
        if(focus){
            Log.d("focus GAINED");
        } else {
            Log.d("focus LOST");
        }
        super.onFocusNotify(focus);
    }
}

And a test. Try various combinations and see what events you receive in the log.

public class TestLifecycle extends MyAppScreen implements FieldChangeListener {
    private final ABNTextEdit txt1;
    private final ButtonField btn1;
    private final ButtonField btn2;

    public TestLifecycle() {

        final Manager manager = getMainManager();

        txt1 = new ABNTextEdit();
        manager.add(txt1);

        btn1 = new ButtonField("Dialog", ButtonField.CONSUME_CLICK);
        btn1.setChangeListener(this);
        manager.add(btn1);

        btn2 = new ButtonField("Screen", ButtonField.CONSUME_CLICK);
        btn2.setChangeListener(this);
        manager.add(btn2);
    }

    public void fieldChanged(final Field field, final int context) {
        if (field == btn1) {
            Dialog.alert("Example alert");
        } else if (field == btn2) {
            UiApplication.getUiApplication().pushScreen(new TestLifecycle());
        }
    }
}

Update:

This method has a limitation: if a new screen is pushed when a dialog or the soft keyboard has focus your current screen will not receive onCovered/onUncovered notification.

Example A: if you have an input field of fixed size and you push a new screen when the user completes it, your current screen will not receive the notification if the user types very quickly. This happens because in the moment between you call push(newScreen) and it is actually pushed the user clicks on a letter on soft KB and it grabs the focus. So only onObscured is called, but not onCovered.

Solution: explicitly hide the soft keyboard before the push(newScreen).

Example B: if you have a customized dialog which pushes new screen and then dismisses itself, your current screen will not receive the notification. This happens because your customized dialog is not recognized as a screen, so only onObscured is called, but not onCovered.

Solution: dismiss the dialog in the first place returning a result value, and let your screen push the new screen based on that value. -OR- override isMyAppScreen() to return true also for your customized dialog.


You should be able to use protected void onExposed() to detect when it is displayed again.

0

精彩评论

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