简体   繁体   English

返回到屏幕的先前版本

[英]Go back to previous version of the screen

I am working on a BlackBerry application that has a number of screens. 我正在使用具有多个屏幕的BlackBerry应用程序。 I am able to navigate back and forth from each screen easily. 我能够轻松地从每个屏幕来回导航。 However, I am trying to navigate "back" to the previous version of the screen. 但是,我试图“返回”到屏幕的先前版本。 For instance, there are two screens A (Home Screen) and B (Second Screen). 例如,有两个屏幕A(主屏幕)和B(第二屏幕)。 I navigate from A to B as follows: 我从A导航到B,如下所示:

UiApplication.getUiApplication().pushScreen(new MsgScreen());

I navigate back to A from B: 我从B导航回A:

public boolean onClose() {
         UiApplication.getUiApplication().popScreen(this);
            return true;
        }

Whenever I navigate from A to B, a new instance of B is created. 每当我从A导航到B时,都会创建B的新实例。 However, screen B has a button that allows it to add LabelFields to a VerticalFieldManager . 但是,屏幕B具有允许其将LabelFields添加到VerticalFieldManager的按钮。 Whenever I navigate from A to B, the fields added are not viewed as it was not part of the constructor. 每当我从A导航到B时,都不会查看添加的字段,因为它不是构造函数的一部分。 Therefore, I would like to view the last or "previous" version of the screen instead of getting a newly created screen each time. 因此,我想查看屏幕的最后一个或“上一个”版本,而不是每次都获取一个新创建的屏幕。 Please help. 请帮忙。

I think you have a couple different choices here (at least): 我认为您至少有两个不同的选择:

Keep Screen B in Memory 将屏幕B保留在内存中

Here, when you create a new instance of B from within class A, you will save it in a member variable (of the A class). 在这里,当您在类A中创建B的new实例时,会将其保存在(A类的)成员变量中。 Then, whenever you need to go back to B from A, you will use the member variable, if there is one: 然后,无论何时需要从A返回到B,都将使用成员变量(如果存在):

Class A: A类:

 private MsgScreen _bScreen;

 private MsgScreen getBScreen() {
     // don't actually create the screen until we need it,
     //   but don't create more than one instance
     if (_bScreen == null) {
         _bScreen = new MsgScreen();
     }
     return _bScreen;
 }

 public void showB() {
     UiApplication.getUiApplication().pushScreen(getBScreen());
 }

Note: see here for a more sophisticated way to implement this technique in many Screen classes, with the use of a reusable base class . 注意:使用可重用的基类请参见此处以更复杂的方式在许多Screen类中实现此技术

Store the State of B and Load it When (Re-)Displayed 存储B的状态并在(重新)显示时加载它

Another option is to let the MsgScreen (B) get destroyed, and re-created every time, as you originally had. 另一个选择是让MsgScreen (B)被破坏,并像您最初那样每次都重新创建。 But, instead of the original design, make sure that when the MsgScreen is closed, you use something like the PersistentStore to save its state. 但是,要确保关闭MsgScreen时没有使用原始设计,而是使用PersistentStore类的东西来保存其状态。 In other words, if the user can add labels on the B screen, then save the content of all the labels. 换句话说,如果用户可以在B屏幕上添加标签,则保存所有标签的内容。 Maybe you save a String[] (or Vector ) to your PersistentStore . 也许您将String[] (或Vector )保存到PersistentStore Then, in the constructor of MsgScreen , or maybe in ( MsgScreen.java ): 然后,在MsgScreen的构造函数中,或者在( MsgScreen.java )中:

protected void onUiEngineAttached(boolean attached) {
    if (attached) {
        // load the String[] from the persistent store, 
        //  and create new LabelFields
    } else {
        // save the String[] to the persistent store
    }
}

you can reload the array of Strings from the persistent store, and create new LabelFields , exactly as you had before. 您可以从持久性存储中重新加载String数组,并像以前一样创建新的LabelFields

Or, you could choose to save the LabelField contents to the store as soon as they are created by the user, instead of when the screen is closed. 或者,您可以选择将LabelField内容由用户创建后立即保存到商店中,而不是在关闭屏幕时保存。 That's up to you. 随你(由你决定。

You might also consider what the content of those labels are, and what should happen if the user leaves your app, and doesn't return (before rebooting). 您可能还会考虑这些标签的内容是什么,以及如果用户离开您的应用并且不返回(在重新启动之前)该怎么办。 Is it ok that the labels' strings are lost? 标签的字符串可以丢失吗?

Also, the first alternative can use up a lot of memory, if you're doing this same thing with many Screens (or if the Screens are complicated, and have many child Fields ). 另外,如果您对多个Screens执行相同的操作(或者如果Screens很复杂并且具有许多子Fields ),则第一种选择可能会占用大量内存。 If you only have this issue with the MsgScreen , then it's probably not a big problem, and the simplest solution would be the first one I suggested. 如果您只在MsgScreen此问题,则可能不是什么大问题,最简单的解决方案是我建议的第一个解决方案。 But, if you have many screens in your app (A, B, C, D, E, F, etc.), then you might not want to save them all, when they're not actually being shown. 但是,如果您的应用中有很多屏幕(A,B,C,D,E,F等),那么当它们实际上没有显示时,您可能不想保存所有屏幕。

These kind of questions should help you determine when, or if, you should save the state of your UI. 这些类型的问题应有助于您确定何时或是否应保存UI的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM