1

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

intent.putExtra("key","value");

to put values that can be received on the next pushed screen in BlackBerry.

Like in Android from ActivityOne -

Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("Value1", "This value one for ActivityTwo ");
intent.putExtra("Value2", "This value two ActivityTwo"); 
startActivity(intent);

in ActivityTwo -

Bundle extras = getIntent().getExtras();
if (extras == null) {
    return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
    // Do something with the data
} 

Is such a thing possible in BlackBerry? If yes then how do I do this?

3 Answers 3

3

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); 
   }
}
7
  • "this doesn't have to be a String..." : this comment is showing how the calling screen would be coupled to the updated screen. If you decide to change _value1 class, you need to change setter methods as well. On the other hand, it is typesafe. Jul 9, 2012 at 7:44
  • @MisterSmith, well, of course you need to change setter methods if the underlying data type changes. That's true of almost any property in almost any class. Very few APIs utilize passing Object as their data type. In some cases, it's warranted, but that's the exception, rather than the rule. In Android, the calling Activity is still coupled to the called Activity in the sense that it knows what data types it's passing. I'm not sure what your comment is here ...
    – Nate
    Jul 9, 2012 at 8:27
  • @MisterSmith, I'll also follow up with an opinion on the Android implementation. I don't think the existing implementation was driven as much by a desire to decouple called Activity classes from the caller. The caller still needs to pass in extras of the correct data type, and the key names still need to be agreed upon by both Activities. A lot of times, those key names are actually implemented as String constants in the called Activity subclass (not the loosest form of coupling). I think the bigger motivation was that Intents allow the OS to manage Activity lifecycles.
    – Nate
    Jul 9, 2012 at 8:44
  • I mean, in Android, the payload is either a Bundle (this one holding only common parcelable types), or a Parcelable or a Serializable. So the container object can be type-agnostic. I'd use a generic container here (like a Hashtable or Vector) so that the calling class doesn't have to include the called class in the imports section. Jul 9, 2012 at 8:58
  • @MisterSmith, Yep, got it. Before I saw your answer, I posted an update above, which is very similar to what you proposed in your answer. I will say, however, that I don't think the import coupling goes away if the calling class still does UiApplication.getUiApplication().pushScreen(), right? But, in Android, the caller is usually using new Intent(this, ActivityTwo.class), so the import requirement is there, too.
    – Nate
    Jul 9, 2012 at 9:04
1

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.

2
  • My apologies. I didn't see your answer when I posted my update. Obviously, I agree with this implementation if you want to create a generic interface. +1!
    – Nate
    Jul 9, 2012 at 8:54
  • thanks @MisterSmith for your valuable solution & comments it really help me. Jul 27, 2012 at 8:38
0

create one public class,

public class Values 
{
    public static String value1 ="";
    public static String value2 ="";
}

Now u can access the value1,and value2 in anywhere.

before push the next screen, assign the values.

public class MyClass extends UiApplication 
{


    MyClass mc = null;
    public static void main(String args[]) 
    {
        mc = new MyClass();
        if (MyClass != null)
        {
            mc.enterEventDispatcher();
        }
    }

    public MyClass() 
    {
               Values.value1 ="HI"
               Values.value2 ="Welcome"
    this.pushScreen(new SplashScreen());
    }

In another class u can access it.

public class Splashscreen()
{
   public Splasscreen()
{
       if(!Values.Value1.equalIgnoreCase("")
{
           Syso(""+Values.value1)   ;
}
}
1
  • This would do the job, but using global data is not recommended. Also is prone to memory leaks, since references are held during the whole app lifetime. Jul 9, 2012 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.