Android Question StartActivityForResult Deprecated

eelias

Active Member
Licensed User
Longtime User
I am trying to use StartActivityForResult in a class however seems it is not possible... what is terrible since I am trying to make classes and not put everything in the main.

However I found the following: https://stackoverflow.com/questions...-method-is-deprecated-what-is-the-alternative

And seems that we are supposed to move away from StartActivityForResult

Is there anyone would care to translate this java code to a inline in the B4A standard? PLEASE PLEASE PLEASE !

So I can use it like this:

Private Sub Transacao(m As Map) Dim stringTransacao As String Dim json As JSONGenerator json.Initialize(m) stringTransacao = json.ToString LogColor("**********************", Colors.Green) LogColor("Transacao", Colors.Green) LogColor(stringTransacao, Colors.Green) LogColor("**********************", Colors.Green) Dim i As Intent i.Initialize(i.ACTION_SEND,"") i.SetComponent("br.com.destaxa.destaxapay/.TransactionActivity") i.PutExtra("transacao",stringTransacao) StartActivityForResult(i) End Sub Private Sub ion_Event (MethodName As String, Args() As Object) As Object Log(Args(0)) Log(Args(1)) If -1 = Args(0) Then 'resultCode = RESULT_OK Dim i As Intent = Args(1) Log(i.ExtrasToString) End If Return Null End Sub:
Private Sub Transacao(m As Map)
    Dim stringTransacao As String
    Dim json As JSONGenerator
    json.Initialize(m)
    stringTransacao = json.ToString
    
    Dim i As Intent
    i.Initialize(i.ACTION_SEND,"")
    i.SetComponent("br.com.destaxa.destaxapay/.TransactionActivity")
    i.PutExtra("transacao",stringTransacao)
    StartActivityForResult(i)
End Sub

Private Sub ion_Event (MethodName As String, Args() As Object) As Object
    Log(Args(0))
    Log(Args(1))
    
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        Log(i.ExtrasToString) 'received data
    End If
    Return Null
End Sub
 
Solution
Example: https://www.b4x.com/android/forum/t...list-of-other-related-methods.129897/#content

Based in this example i could make it run as class in my big app:

B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN,"br.com.destaxa.destaxapay")
    i.SetComponent("br.com.destaxa.destaxapay/.TransactionActivity")
    i.PutExtra("transacao",stringTransacao)
    StartActivityForResult(i)
    Wait For ion_Event (MethodName As String, Args() As Object)
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        LogColor(i.ExtrasToString, Colors.Green)
    Else
        LogColor("Transacao Falhou", Colors.Red)
    End If
End Sub

eelias

Active Member
Licensed User
Longtime User
The code I posted above works in a small sample app

When integrating it as a class in my big (no b4xpage) app it does not even bring the intent app to work.

I am very confused on how to procced on that.

The documentation is this:

B4X:
// Criar o Intent
Intent mDestaxaPay = new Intent();
// Definir a Class e Activity que serão executados
mDestaxaPay.setClassName("br.com.destaxa.destaxapay",
"br.com.destaxa.destaxapay.TransactionActivity");
// Converter o JSON da transação para String
String stringTransaction = sdkTransaction.toString();
// Carregar os dados da transação
mDestaxaPay.putExtra(“transacao”, stringTransaction);
// Executar o DestaxaPay e aguardar o retorno
startActivityForResult(mDestaxaPay, PROCESS_TRANSACTION);

In a simples test app this works:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

    #AdditionalJar: libgedi-1.16.8-gpos700-payment-release.aar
    #AdditionalJar: ppcomp_release-1.29.14.210610.aar
    
    #SignKeyAlias: developmentgertecdeveloper_enhancedapp
    #SignKeyFile: C:\Dev\yashar\Destaxa\Development_GertecDeveloper_EnhancedAPP.keystore
    #SignKeyPassword: [email protected]

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private ion As Object

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("layout")
End Sub


Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Sub SendDestaxa(m As Map)
    Dim stringTransacao As String
    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 ButtonAtivar_Click
    
    Dim m As Map
    m.Initialize
    m.Put("operacao", "ativar")
    m.Put("documento", "xxxxxxx")

    ShowPicker(m)
End Sub

Private Sub ButtonPagar_Click
    Dim m As Map
    m.Initialize
    m.Put("operacao", "pagar")
    m.Put("forma", "credito")
    m.Put("valor", 1)
    m.Put("parcelas", 1)
    m.Put("juros", False)

    ShowPicker(m)
End Sub

Sub ShowPicker(m As Map)
    Dim stringTransacao As String
    Dim json As JSONGenerator
    json.Initialize(m)
    stringTransacao = json.ToString
    
    
    Dim i As Intent
    i.Initialize(i.ACTION_SEND,"")
    i.SetComponent("br.com.destaxa.destaxapay/.TransactionActivity")
    i.PutExtra("transacao",stringTransacao)
    StartActivityForResult(i)
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
    Log(Args(0))
    Log(Args(1))
    
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        Log(i.ExtrasToString)
    End If
    Return Null
End Sub

Sub GetIntentData(i As Intent) As String
    Dim r As Reflector
    r.Target = i
    Return r.RunMethod("getDataString")
End Sub

This works somehow in a test app, bringing it to a class on a bigger app does not work.

I tried your example for using wait for, it returns with 0 as resulcode always and do not start the intent. Same android, POS, etc
 
Last edited:
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
The only difference I see is how you're invoking the StartActivityForResult. In your example that does not work, you are supplying an extra parameter that is not supplied in the working example.

Have you tried removing the second parameter from your non-working example to see if it invokes the intent?
 
Upvote 0

eelias

Active Member
Licensed User
Longtime User
Example: https://www.b4x.com/android/forum/t...list-of-other-related-methods.129897/#content

Based in this example i could make it run as class in my big app:

B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN,"br.com.destaxa.destaxapay")
    i.SetComponent("br.com.destaxa.destaxapay/.TransactionActivity")
    i.PutExtra("transacao",stringTransacao)
    StartActivityForResult(i)
    Wait For ion_Event (MethodName As String, Args() As Object)
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        LogColor(i.ExtrasToString, Colors.Green)
    Else
        LogColor("Transacao Falhou", Colors.Red)
    End If
End Sub
 
Upvote 0
Solution
Top