Android Question How to pass current activity as parameter

Discussion in 'Android Questions' started by shantharam, Aug 30, 2015.

  1. shantharam

    shantharam New Member Licensed User

    I have seen the following code under thread - Take Screenshot in B4A version 2.0 (posted by another user)

    Sub TakeScreenshot (Activity As Activity)

    My question is how do I pass the current activity as a parameter to the subroutine TakeScreenshot(Activity as Activity)

    Any help is appreciated. In Java I believe the this reference will do the trick but how to pass the Main activity to this subroutine.

    The code for the subroutine is as follows
    Sub TakeScreenshot (activity As activity)
    Log("TakeScreenshot activated")
    ' Take a screenshot.

    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Dim now, i As Long
    Dim dt As String
    DateTime.DateFormat = "yyMMddHHmmss"
    now = DateTime.now
    dt = DateTime.date(now) ' e.g.: "110812150355" is Aug.12, 2011, 3:03:55 p.m.
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
    bmp.InitializeMutable(activity.Width,activity.Height)
    c.Initialize2(bmp)
    Dim args(1) As Object
    Dim types(1) As String
    Obj2.Target = c
    Obj2.Target = Obj2.GetField("canvas")
    args(0) = Obj2.Target
    types(0) = "android.graphics.Canvas"
    Obj1.RunMethod4("draw", args, types)
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirRootExternal, dt & ".png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.close
    ToastMessageShow("Screenshot Captured", False)
    End Sub
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    If this sub is in the activity module then just delete the parameter from the sub signature and it will work.
     
  3. shantharam

    shantharam New Member Licensed User

    Thanks Erel for the reply. Will use the code tag for future questions.

    With regards to the above question the sub is not in the activity module but in a separate module so that it can be reused between multiple activities. In that case how can the activity be passed. Any help is appreciated.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can call it with:
    Code:
    OtherModule.TakeScreenshot (Activity)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice