I assume you've already looked at our answers to this question? If not, please read that first.
Background
BlackBerry doesn't use Intent
objects, and there is no direct equivalent. In Android, Intent
objects are used for different reasons:
- to simply start an
Activity
- to decouple the caller of the
Activity
from the Activity
itself
- to help Android manage the lifecycle (create/suspend/resume/destroy) of
Activity
objects, which it could not do if people just created them directly with new MyActivity();
So, the question is, for which of those three reasons (or what other reason) are you asking? My first guess would be #1 ... you just want to be able to start the activity.
First of all, BlackBerry doesn't have the same concept of an Activity
. In Android, you have a View layer, which is normally represented by your XML layouts, possibly with some custom Java classes that extend the Android View
class.
Then, Android has an Activity
layer, which is similar to the Controller in a Model-View-Controller pattern. The Activity
shouldn't be too tightly coupled to the UI (the View), and ideally, it shouldn't contain the back-end data, which should live in a data Model layer.
This can be really nice, because programmers can work on the Model and Controller (Activity
) code, and graphical UI designers who don't know Java can work on the visual design, entirely in XML (and the Layout Editor tool).
BlackBerry doesn't have anything like Android's View layer. To build a BlackBerry UI, if you're using the native BlackBerry Java platform and tools, you have to do it in Java code. So, for that reason, I find that fewer BlackBerry projects work hard to separate the Controller layer from the View layer. Also, I think Model-View-Controller is more important the bigger a software project gets, and relative to desktop applications, smartphone apps are often smaller. So, for better or worse, I think that's a reason that MVC isn't built into BlackBerry APIs more directly.
One Solution
So, as your code snippet implies, a lot of people put the logic that would be in an Android Activity
subclass, into a Screen
subclass in BlackBerry. It more closely couples the controller logic with the UI, but sometimes that's acceptable.
If that's what you're comfortable with, then often you will launch your Screens (Activities) with code as simple as this:
StatusActivity nextScreen = new StatusActivity();
nextScreen.setFirstName(strsessionFirstName);
UiApplication.getUiApplication().pushScreen(nextScreen);
The logic that an Android Activity
would have in the onCreate()
method, to inspect Intent
extras, and setup the view, could be in the constructor of StatusActivity
. If some of the UI initialization depends on the value of strsessionFirstName
, then you can setup that UI within the setFirstName()
method:
StatusActivity.java:
private LabelField _nameLabel;
public void setFirstName(String value) {
if (_nameLabel == null) {
_nameLabel = new LabelField(value);
add(_nameLabel); // add the new field to the screen
} else {
_nameLabel.setText(value);
}
}
Or, you could pass the firstName String
into the StatusActivity
constructor, and set it up then.
Another Solution
What Mister Smith was suggesting in the other question I linked to above, was to create a generic interface for all your BlackBerry "activities", where you set properties with key-value pairs. So, instead of implementing setFirstName()
, you would do
StatusActivity.java:
private Hashtable _extras = new Hashtable();
private LabelField _nameLabel;
public void putExtra(String name, Object value) {
_extras.put(name, value);
}
// NOTE: this method is private now, and can only be called from inside this class!
private void setFirstName(String value) {
if (_nameLabel == null) {
_nameLabel = new LabelField(value);
add(_nameLabel); // add the new field to the screen
} else {
_nameLabel.setText(value);
}
}
// this overrides a base class method, and is called with the screen is displayed
public void onUiEngineAttached(boolean attached) {
super.onUiEngineAttached(attached);
if (attached) {
String firstName = _extras.get("first_name");
setFirstName(firstName);
}
}
You could then start the StatusActivity
like this:
StatusActivity nextScreen = new StatusActivity();
nextScreen.putExtra("first_name", strsessionFirstName);
UiApplication.getUiApplication().pushScreen(nextScreen);