How to listen to a closed screen event

Hi, do achieve the example that I gave to 2 screens, I would class to know when App screen1 is closed and act at this time, how can I do this?

import net.rim.device.api.ui.UiApplication;

public class App extends UiApplication{

    public static void main(String[] args)    {        App  app = new App();        app.enterEventDispatcher();    }    public App()    {        pushScreen(new Screen1());        pushScreen (new Screen2());    }}

Screen1.Java:

import net.rim.device.api.ui.component.LabelField;import net.rim.device.api.ui.container.MainScreen;

final class Screen1 extends MainScreen{

    public Screen1()    {        super();         setTitle(new LabelField("Screen1"));    }    public boolean onClose()    {     setDirty(false);      return super.onClose();    }

}

Screen2.Java:

import net.rim.device.api.ui.component.LabelField;import net.rim.device.api.ui.container.MainScreen;

final class Screen2 extends MainScreen{

    public Screen2()    {        super();         setTitle(new LabelField("Screen2"));    }    public boolean onClose()    {        setDirty(false);      return super.onClose();    }}

Declare an interface:

public interface ScreenClosedListener {

   public void notifyScreenClosed(MainScreen screen);

}

Add features to the Screen1 class.

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

final class Screen1 extends MainScreen {

    private ScreenClosedListener listener;

    public Screen1(ScreenClosedListener listener) {
        super();
        setTitle(new LabelField("Screen1"));
        this.listener = listener;
    }

    protected void onUndisplay() {       listener.notifyScreenClosed(this);    } 

    protected boolean onSavePrompt() {
      // if you want to always close the screen
      // without questions just return true
      // there is no need in setDirty(false)
      // in onClose() method

      return true;
    }
}

Add features to the App class, make the screen closed event listener

import net.rim.device.api.ui.UiApplication;

public class App extends UiApplication implements ScreenClosedListener {
       // compose Screen1 instance and pass to the constructor    // the reference to the App clas    // which is ScreenClosedListener implementation     private Screen1 screen1Instance = new Screen1(this); 

    public static void main(String[] args) {
        App  app = new App();
        app.enterEventDispatcher();
    }

    public App() {        pushScreen(screen1Instance);
        pushScreen (new Screen2());
    }

   public void notifyScreenClosed(MainScreen screen) {

         // check the event source object         if (screen==screen1Instance) {     // act accordingly here     }   }

}

}

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for