Starting with B4A V1.1 libraries can use startActivityForResult and receive the onActivityResult event.
There is a new interface named: IOnActivityResult.
You should implement this interface and pass it to BA.startActivityForResult:
This method starts the intent and also takes care of mapping this request with the given iOnActivityResult.
To avoid memory leaks this method only holds a WeakReference to iOnActivityResult. Which means that you need to hold a strong reference to it in your code.
When the result arrives the IOnActivityResult object will be called with the resultCode and Intent values.
Here is an example taken from VoiceRecognition object:
ion is an instance variable.
When the user calls Listen we create a new Intent with the required values.
We also initialize ion. The action done when the result arrives is to take the values from the result Intent and raise the "Result" event.
You should always check resultCode and make sure it is RESULT_OK.
The last step is to actually start the activity by calling ba.startActivityForResult.
There is a new interface named: IOnActivityResult.
B4X:
public interface IOnActivityResult {
void ResultArrived(int resultCode, Intent intent);
}
B4X:
public synchronized void startActivityForResult(IOnActivityResult iOnActivityResult, Intent intent)
To avoid memory leaks this method only holds a WeakReference to iOnActivityResult. Which means that you need to hold a strong reference to it in your code.
When the result arrives the IOnActivityResult object will be called with the resultCode and Intent values.
Here is an example taken from VoiceRecognition object:
B4X:
private IOnActivityResult ion;
/**
* Starts listening. The Ready event will be raised when the result arrives.
*/
public void Listen(final BA ba) {
if (eventName == null)
throw new RuntimeException("VoiceRecognition was not initialized.");
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
if (prompt != null && prompt.length() > 0)
i.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
if (language != null && language.length() > 0)
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
ion = new IOnActivityResult() {
@SuppressWarnings("unchecked")
@Override
public void ResultArrived(int resultCode, Intent intent) {
List list = new List();
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> t = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (t.size() > 0) {
list.setObject((java.util.ArrayList)t);
}
}
ba.raiseEvent(VoiceRecognition.this, eventName + "_result", list.IsInitialized(), list);
}
};
ba.startActivityForResult(ion, i);
}
When the user calls Listen we create a new Intent with the required values.
We also initialize ion. The action done when the result arrives is to take the values from the result Intent and raise the "Result" event.
You should always check resultCode and make sure it is RESULT_OK.
The last step is to actually start the activity by calling ba.startActivityForResult.