Android Question StartActivityForResult and getting the results

EduardoElias

Well-Known Member
Licensed User
Longtime User
I have implemented the following code to interact with a Intent for POS system. It does execute correctly but it always returns arg(0) = 0 and I dont get back the data needed.

It returns a string with a JSON inside allways


B4X:
Private Sub ButtonAtivar_Click
  
    Dim stringTransacao As String
    Dim m As Map
    m.Initialize
    m.Put("operacao", "ativar")
    m.Put("documento", "21.333.xxx/xxxx-xx")
  
    Dim json As JSONGenerator
    json.Initialize(m)
    stringTransacao = json.ToString

    Dim pac As PackageManager
  
    Dim in As Intent

    in=pac.GetApplicationIntent("br.com.destaxa.destaxapay")
    in.PutExtra("transacao", stringTransacao)
    StartActivityForResult(in)
End Sub

private Sub ion_Event (MethodName As String, Args() As Object) As Object
    If Args(0) = 0 Then 
       ??????
    End If
    Return Null
End Sub

This is based on the example from the documentation :

B4X:
Intent mDestaxaPay = new Intent();
mDestaxaPay.setClassName("br.com.destaxa.destaxapay",
"br.com.destaxa.destaxapay.TransactionActivity");
String stringTransaction = sdkTransaction.toString();
mDestaxaPay.putExtra(“transacao”, stringTransaction);
startActivityForResult(mDestaxaPay, PROCESS_TRANSACTION);

The documentation presents the following as the result for the above code and get the string data expected:

B4X:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PROCESS_TRANSACTION && resultCode == RESULT_OK) {
try {
JSONObject response = new JSONObject(data.getStringExtra(“resposta”));
processResponse(response);
} catch (JSONException e) {
}
}
}

The service is called based on the B4A that I implemented, however there is no useful return. I get a arg(0) = 0 and arg(1) = null.

I need to retrieve the result
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
ion_Event does work. i use it frequently. you don't show any actual code in your implementation of ion_Event.

first you say you get a result:
"Only get the event ion_event with result and only 1 param as 0 the second is null"

then you say you don't get a result:
"It does work, it reads my string (json) and execute just fine. However I do not get the result"

which is it? result? or not result? please clarify: exactly what do you receive in args() in ion_Event?

if args(0) is 0 then something is wrong. if args(1) is null, then the other activity is not returning values
in the expected order. args(1) is the intent with the returned data from the other activity.

do you know exactly what the other activity is supposed to return?


in io_Event, can you see the intent?
Dim i As Intent = Args(1)

if yes, then, add:
Log( "all extras: " & i.ExtrasToString )


see what you see.
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
ion_Event does work. i use it frequently. you don't show any actual code in your implementation of ion_Event.

first you say you get a result:
"Only get the event ion_event with result and only 1 param as 0 the second is null"

then you say you don't get a result:
"It does work, it reads my string (json) and execute just fine. However I do not get the result"

which is it? result? or not result? please clarify: exactly what do you receive in args() in ion_Event?

do you know exactly what the other activity is returning?


in io_Event, can you see the intent?
Dim i As Intent = Args(1)

if yes, then, add:
Log( "all extras: " & i.ExtrasToString )


see what you see.

the EVENT ion_Event is triggered

but the results are:

arg(0) = 0 and arg(1) = null

and I am sure that the intent was executed and there is a return string to be used... I dont know how
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you say: "they suggest this return:" who is "they"? is there some documentation somewhere?

the so called "result" is an intent (with its data). ion_Event references it in args(1). where is the documentation?

by the way, arg(0) = 0 is not RESULT_OK. RESULT_OK is -1. so other activity is responding, but result is not ok...
in other words, this is wrong:
B4X:
    If Args(0) = 0 Then
       ??????
    End If
    Return Null

you want if Args(0) = -1 for a successful response. "if Args(0) = 0 then ..." means you are only interested in examining failure.
just log("Args(0): " & Args(0)) if you want to know the status of the response.

the suggested java solution ("they suggested ...") doesn't quite work with b4a. b4a's version of onActityResult changes things a little. it is possible to do the whole thing in inline java (startActivityForResult/onActivityResult) but you have to work with b4a's IOnActivityResult interface.

but everything already works in b4a so there is no need for inline java in this case. without documentation or access to the app itself, i really cannot test anything. i have tested both in b4a and inline java with an app that returns a result from startActivityForResult, so i know things work. either you have not
set things up correctly or the app is returning something unexpected. what little code you posted seems to be ok, but the whole story is not clear.
 
Last edited:
Upvote 0
Top