[wish] StartActivityForResult and OnActivityResult

Eduard

Active Member
Licensed User
Longtime User
Support for StartActivityForResult and OnActivityResult
The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.
Activity | Android Developers

I think this is one of the most important features of Android.
 

Eduard

Active Member
Licensed User
Longtime User
Thanks. I really hope it will be supported at basic level too very soon.
 

kanaida

Active Member
Licensed User
Longtime User
I really hope this happens soon as well. This is part of the core way of dealing with intents. Like getting a photo back from a camera in a few lines of code.
 

thedesolatesoul

Expert
Licensed User
Longtime User
Try the library in the attachment.

However remember you cannot use MsgBox etc in Activity_OnResult. So use CallSubDelayed.

B4X:
    Dim AU As MyActivityUtils
        Dim In As Intent
        In.Initialize("com.maximussoft.browser.intent","")
        In.PutExtra("Browse",0)
        AU.startActivitywithResult(In)

B4X:
Sub Activity_OnResult(ResultCode As Int, ReturnIntent As Intent)
    Log(ResultCode & " " & ReturnIntent.ExtrasToString)
    If ResultCode = 0 Then Return   
    If ReturnIntent.HasExtra("Path") Then
        et_remote_loc.Text = "dropbox:/" & ReturnIntent.GetExtra("Path")
    End If
   
End Sub
 

Attachments

  • sAR.zip
    3.5 KB · Views: 560

Big JR

Member
Licensed User
Longtime User
Try the library in the attachment.

However remember you cannot use MsgBox etc in Activity_OnResult. So use CallSubDelayed.

B4X:
    Dim AU As MyActivityUtils
        Dim In As Intent
        In.Initialize("com.maximussoft.browser.intent","")
        In.PutExtra("Browse",0)
        AU.startActivitywithResult(In)

B4X:
Sub Activity_OnResult(ResultCode As Int, ReturnIntent As Intent)
    Log(ResultCode & " " & ReturnIntent.ExtrasToString)
    If ResultCode = 0 Then Return  
    If ReturnIntent.HasExtra("Path") Then
        et_remote_loc.Text = "dropbox:/" & ReturnIntent.GetExtra("Path")
    End If
  
End Sub

The ResultCode & " " & ReturnIntent.ExtrasToString is always '0 not initialized' whether I choose the messaging or email app from the share choose dialog. I'm initializing the intent as In.Initialize("android.intent.action.SEND", ""). What am I missing?
 

thedesolatesoul

Expert
Licensed User
Longtime User
The ResultCode & " " & ReturnIntent.ExtrasToString is always '0 not initialized' whether I choose the messaging or email app from the share choose dialog. I'm initializing the intent as In.Initialize("android.intent.action.SEND", ""). What am I missing?
Are you sure the 'SEND' intent is supposed to return some data back?
 

Big JR

Member
Licensed User
Longtime User
Are you sure the 'SEND' intent is supposed to return some data back?
Ah. I was hoping that all intents returned some data so I could trap if the user actually went ahead and sent the mms or aborted by pressing the back button. I may have to resort to a broadcast receiver.
 

JordiCP

Expert
Licensed User
Longtime User
Hi @thedesolatesoul

I realised that I am using the sAR library you posted in my project. I cannot find it in the official libraries.

Is it yours? Is it free to use? Attribution credits or other type of licensing?


Thank you!
 

thedesolatesoul

Expert
Licensed User
Longtime User
Hi @thedesolatesoul

I realised that I am using the sAR library you posted in my project. I cannot find it in the official libraries.

Is it yours? Is it free to use? Attribution credits or other type of licensing?


Thank you!
Its free to use any way you please.
Just post back suggestions if you have any.
 

Steini1980

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_OnResult(ResultCode As Int, ReturnIntent As Intent)
    Log(ResultCode & " " & ReturnIntent.ExtrasToString)
    setImageRelation(ReturnIntent.GetExtra("de.dirkfarin.imagemeter.image_id"), ActualPicture)
End Sub

Sub ImageMeterSetImage(Dateiname As String)
  Dim In As Intent
  In.Initialize(In.ACTION_SEND,"")
  In.SetComponent("de.dirkfarin.imagemeterpro/.lib.ActivityLoader")
  In.setType("image/jpeg")
  In.PutExtra("android.intent.extra.STREAM",CreateUri("file://" & File.Combine(File.DirDefaultExternal, Dateiname)))
  In.PutExtra("de.dirkfarin.imagemeter.folder_name","Projekte MDE")
  In.PutExtra("de.dirkfarin.imagemeter.image_name",Dateiname)
  In.PutExtra("de.dirkfarin.imagemeter.quiet_import",True)
  AU.startActivitywithResult(In)
End Sub

@thedesolatesoul:
I'm trying to use you sAR-Lib, but Debugger throws Exception at setImageRelation() line:
java.lang.RuntimeException: Object should first be initialized (Intent).

Programm paused on line: 348
setImageRelation(ReturnIntent.GetExtra("de.dirkfarin.imagemeter.image_id"),ActualPicture)
 
Last edited:

henrywood

Active Member
Licensed User
Longtime User
Can I use an activity of my own app as the activity that opens when calling StartActivityWithResult ?

I tried this in the click handler that should open the activity

B4X:
Sub b_Click

   Dim AU As MyActivityUtils
  Dim In As Intent
     In.Initialize(In.ACTION_PICK, "")
  In.PutExtra("LIST", "friends")
  In.SetComponent("com.b4a.example.act2") ' act2 is the name of my activity in B4A and 'com.b4a.example' is the Package name ?
   AU.startActivitywithResult(In)

End Sub

Sub Activity_OnResult(ResultCode As Int, ReturnIntent As Intent)

Log(ResultCode & " " & ReturnIntent.ExtrasToString)

If ResultCode = 0 Then Return

If ReturnIntent.HasExtra("Path") Then
  b.Text = "dropbox:/" & ReturnIntent.GetExtra("Path")
End If

End Sub

but the log just says:


onActivityResult: wi is null
 
Top