BlackBerry - how to pass data like Intent.putExtra() in Android

后端 未结 3 1568
甜味超标
甜味超标 2021-01-24 16:07

I am newbie on BlackBerry. How do I achieve (in BlackBerry) the same as that of Android\'s

intent.putExtra(\"key\",\         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 17:02

    This is something worth designing well once because you'll end using it on most projects.

    To begin with, no there's no built-in mechanism such as Android's in BB, but you can (and should) code your own:

        public class MyScreen extends MainScreen {
    
            public void updateParams(Hashtable params){
                // Read from hashtable and cast
                // Update fields (on Ui thread)
            }
    
        }
    

    As you can see, I've used a hashtable because it is the most flexible way. You could use setters, but then you'd be coupling the calling screen to the updated screen class. This allows you to pass a single or several parameters. You could have used a Object[], and thus save a few references, but that optimization hardly pays off and you would be coupled to the array's lenght as well as to the order of objects inside the array. Now, to pass two params to a screen, you would do:

        Hashtable ht = new Hashtable();
        ht.put("somestring", "Hello!");
        ht.put("someint", new Integer(3));
    
        MainScreen ms = new MyScreen();
        targetscreen.updateParams(ht);
        // Now push screen
    

    You could also create a constructor like this:

    Myscreen(Hashtable params)
    

    But this forces you to create a new instance each time you need to update the screen with new data. With the method, you could update a screen which is already on the stack.

    This is an approach you could reuse in many projects. In fact, most times you'll end subclassing MainScreen anyway to abstract and simplify repetitive tasks like Menu creation and handling key presses, so this would be a part of that subclass.

提交回复
热议问题