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());
}
}
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With