1

I am trying to figure out what the equivalent of "Android Intent" is in BlackBerry Java. I have read similar questions/answers to this but I have been unable to use them. One suggestion was having a method

public class StatusActivity extends MainScreen {

    String strResult;
    String strsessionFirstName;

    public void setResult(String value) {
        strResult = value;
    }
    public void setFirstName(String value) {
        strsessionFirstName = value;
    }

which is called from the main class like

if(strResult.equals("credentialaccepted"))
{
    StatusActivity nextScreen = new StatusActivity();
    nextScreen.setFirstName(strsessionFirstName);

But this is not working for me. The other option of using Hashtable, I have been unable to comprehend. Can anyone explain with sample code? Thanks.

Nate
  • 31,017
  • 13
  • 83
  • 207
Sarah
  • 1,895
  • 2
  • 21
  • 39
  • What is not working for you? Why don't you use Ani object and pass it to the main screen in the main screens constructor ? – rfsk2010 Jul 11 '12 at 07:07
  • What is not working is that it is not reading the values. The value remains "null". How should I use an object to pass? – Sarah Jul 11 '12 at 07:15

1 Answers1

2

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:

  1. to simply start an Activity
  2. to decouple the caller of the Activity from the Activity itself
  3. 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);
Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • Thank you Nate. Finally got it working. My issue was the values were not being read in "StatusActivity". Finally working now. Thanks again. – Sarah Jul 11 '12 at 08:04