Android Question code modules, activities and scopes

sorex

Expert
Licensed User
Longtime User
Hello,

Since the switching between activities is rather slow I was thinking of going for code modules to have my code seperated as much as possible and work with swappable panels instead.

I remember from a previous project that code modules behave different so I have a few questions about them.

As my main activity can't be used with commands like main.addview() I tried to pass the activity to the sub and store it in a variable so that it's reachable from the other sub in the code module aswell without the need to pass it to all subs.

B4X:
Dim dst As Activity

Sub downloaddata(dstact)
dst=dstact
End Sub

but then I get "Can not access activity from sub"

is there a way to add view to my "Main" activity from in a code module?

or do I have to call a sub in my main activity just for the view adding?
 

sorex

Expert
Licensed User
Longtime User
I know, but as I can access variables with Main.variable name I hoped that Main.addview would work aswell.

if I want to store that parameter so that other subs know the activity aswell what type do I need? Activity or Object?

Dim dst As Activity in the globals still gives that "Can not access activity from sub" error
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I was affraid to get this answer.

If I call a sub in my main activity will the scope still be the code module or is the scope where the "remote" sub is located?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
another weird issue.

in my code module I have this

B4X:
Sub downloaddata()
Log("download file")
Main.dljob.Initialize("update","Main")
Main.dljob.Download("http://someurl")
End Sub

when I place the job done sub in Main it works right.

when I change the code to

B4X:
dljob.Initialize("update","maindata")  'maindata = codemodulename
dljob.Download("http://someurl")

and place the jobdone inside the codemodule the app crashes.


Is this another limitation or did I do something wrong?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Klaus,

When I use

B4X:
Sub getcontent(act As Activity,img As View)

and

B4X:
  maindata.getcontent(Me,imgLogo)

as call I get compile errors...

maindata.getcontent(Me,imgLogo)
javac 1.7.0_07
src\xxx\com\main.java:470: error: inconvertible types
mostCurrent._maindata._getcontent(mostCurrent.activityBA,(anywheresoftware.b4a.objects.ActivityWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.ActivityWrapper(), (anywheresoftware.b4a.BALayout)(main.getObject())),(anywheresoftware.b4a.objects.ConcreteViewWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.ConcreteViewWrapper(), (android.view.View)(mostCurrent._imglogo.getObject())));
^
required: BALayout
found: Class<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?

I started to happen when I added the activity parameter.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I forgot to say that (img) is a view where the placement of the new buttons depends on.

So I still need an addview to the activity there aswell.

How would I reference to the main activity when ME ain't right?

The "problem code" is below...

B4X:
For i = 0 To content.Size - 1
rowmap = content.Get(i)
btnMenu.Initialize("menubutton")
Main.AddView(btnMenu,0,0,0,0)    ' <- I need this to work
btnMenu.Width=bw
btnMenu.Height=bh
btnMenu.Left=(100%x-bw)/2
btnMenu.Top=(img.Top+img.Height)+20+(bh*i)
btnMenu.Text=rowmap.Get("cName")
btnMenu.Tag=rowmap.Get("cName")
Next
 
Upvote 0
Top