Android Code Snippet Using StartActivityForResult with JavaObject

SubName: StartActivityForResult

Description: This code demonstrates how JavaObject can be used to call external APIs that should be called with Context.startActivityForResult.
Same implementation in a class: https://www.b4x.com/android/forum/threads/b4xpages-and-startactivityforresult.119176/post-745271

This code should be added to an Activity. You should also declare a process global variable named ion:
Code:
Sub Process_Globals
   Private ion As Object
End Sub

Code:
Sub ion_Event (MethodName As String, Args() As Object) As Object
'Args(0) = resultCode
'Args(1) = intent
Return Null
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

For example we can show the Ringtone picker with this code:
Code:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     ShowPicker
   End If  
End Sub

Sub ShowPicker
   Dim i As Intent
   i.Initialize("android.intent.action.RINGTONE_PICKER", "")
   i.PutExtra("android.intent.extra.ringtone.TYPE", 1)
   StartActivityForResult(i)
End Sub


Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then 'resultCode = RESULT_OK
     Dim i As Intent = args(1)
     Dim jo As JavaObject = i
     Dim uri As String = jo.RunMethod("getParcelableExtra", _
       Array As Object("android.intent.extra.ringtone.PICKED_URI"))
     Log(uri)
   End If
   Return Null
End Sub

Dependencies: JavaObject v1.20+

Tags: StartActivityForResult, OnActivityResult
 
Last edited:
I started to prepare sample and found the root of problem.
I used local variable:
Code:
Private IOnActivityResult As Object = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "IOnActivityResult", Null)

while the original code used global one

Code:
Sub Process_Globals
   Private ion As Object
End Sub

I changed local to global and it works now correct.
 
April 2014 !!!
It is possible to use it also to call internal activities? (Yes, you've already responded to me that I can use CallSubDelayed, but you did not developed this at that time :))

There are no advantages for this method over CallSubDelayed. CallSubDelayed is simpler to use and is actually more powerful (for internal activities).

There could be an advantage: you could use it to know if the Activity was resumed.

"Recently" ;) I have had problems about this; using startActivity, the following instructions will be executed immediately, without knowing if the Activity was completely activated (Activity_Resume raised).

So, if you can implement StartActivityForResult even for B4A Activities (and if it waits)...


Thank you
 
Hey Erel,
If using this with a share intent, can I determine whether sharing was successful ?
 
I'm using the share intent

Code:
Dim in As Intent
in.Initialize(in.ACTION_SEND, "")
in.PutExtra("android.intent.extra.TEXT", "This is my text to send.")
in.SetType("text/plain")
in.WrapAsIntentChooser("Share Via")
StartActivity(in)
 
Hello,

how can I open this pircker with a specific item selected?

SubName: StartActivityForResult

Description: This code demonstrates how JavaObject can be used to call external APIs that should be called with Context.startActivityForResult.

This code should be added to an Activity. You should also declare a process global variable named ion:
Code:
Sub Process_Globals
   Private ion As Object
End Sub

Code:
Sub ion_Event (MethodName As String, Args() As Object) As Object
'Args(0) = resultCode
'Args(1) = intent
Return Null
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

For example we can show the Ringtone picker with this code:
Code:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     ShowPicker
   End If  
End Sub

Sub ShowPicker
   Dim i As Intent
   i.Initialize("android.intent.action.RINGTONE_PICKER", "")
   i.PutExtra("android.intent.extra.ringtone.TYPE", 1)
   StartActivityForResult(i)
End Sub


Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then 'resultCode = RESULT_OK
     Dim i As Intent = args(1)
     Dim jo As JavaObject = i
     Dim uri As String = jo.RunMethod("getParcelableExtra", _
       Array As Object("android.intent.extra.ringtone.PICKED_URI"))
     Log(uri)
   End If
   Return Null
End Sub

Dependencies: JavaObject v1.20+

Tags: StartActivityForResult, OnActivityResult
 
Back
Top