Using public static
variables (as in Ganesh's answer) will work, but in general, that's not an object-oriented pattern that I would recommend.
Luckily, this can be easier in BlackBerry than in Android. Android's Intent
mechanism is actually somewhat unnatural to many Java developers. When one Activity
starts another Activity
via an Intent
, it does not actually new
up (create) the instance of the second Activity
, and it also doesn't hold a normal Java reference to that object. If it did, this problem would be much simpler. Android's implementation forces you to use the intent extras mechanism.
If your old ActivityOne
class becomes ScreenOne
for BlackBerry, and ActivityTwo
becomes ScreenTwo
, then you can just do something like this:
public class ScreenTwo extends Screen {
private String _value1; // this doesn't have to be a String ... it's whatever you want
private String _value2;
public void setValue1(String value) {
_value1 = value;
}
public void setValue2(String value) {
_value2 = value;
}
}
Then, in ScreenOne
, you can start ScreenTwo
this way
ScreenTwo nextScreen = new ScreenTwo();
nextScreen.setValue1("This value one for ActivityTwo");
nextScreen.setValue2("This value two ActivityTwo");
UiApplication.getUiApplication().pushScreen(nextScreen);
That's actually more consistent with the way Java objects normally are used, and interact with one another.
There's good reasons why Android made Intents
and extras, but in BlackBerry, you just don't have to worry about that.
Edit: I'm trying to consider what I think is the motivation behind Mr. Smith's comment below. If you actually like the Android Intent
extras mechanism in the sense that you can pass multiple data types from one Activity
to another, as key-value pairs, then you can certainly achieve something similar in BlackBerry. Instead of the ScreenTwo
code above, you could use this:
public class ScreenTwo extends Screen {
private Hashtable _extras;
public void putExtras(Hashtable extras) {
_extras = extras;
}
}
Where you put(Object, Object)
key-value pair data into a Hashtable
passed to the called screen, and then read it when you need it. Or even:
public class ScreenTwo extends Screen {
private Hashtable _extras;
public void putExtra(String name, Object value) {
_extras.put(name, value);
}
}