Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine if the data being sent back from an activity is a String or String[]?

I am launching an activityForResult() from my MainActivity. Depending one the option the user selects in the SecondActivity it returns either a String or a String[]. In my MainActivity I override onActivityResult(), but how can I test the returned data first to see if it's a String or String[] so I can handle it accordingly?

This is how I'm handling the Array:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub

                    String[] result =data.getStringArrayExtra("elements");
                    et.setText("");
                    for(int i=0;i<result.length;i++){
                        et.append(result[i].toString());
            }
            }
like image 779
RapsFan1981 Avatar asked Dec 01 '25 08:12

RapsFan1981


2 Answers

Why not always return a String[] from the SecondActivity? If there is only a single String, simply return a String [] with that one String in it. This seems like bad design to return two different types of objects with the same key...

like image 91
Steven Byle Avatar answered Dec 02 '25 23:12

Steven Byle


I would put those in different key. Something like element to the String, and elements to a String[].

String[] result =data.getStringArrayExtra("elements");
if(result == null)
   String strResult = data.getStringExtra("element");

I also supports what Steven said. But don't know the design of your app and the reason you do it like that, so you have to make the choice.

like image 21
Elad92 Avatar answered Dec 02 '25 23:12

Elad92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!