Android Question Multiple Activities, Same View, Same Event

mesutaslan

Member
Licensed User
Longtime User
I have same view in multiple activities (For Ex: Main menu button). I can clone event code from one activity to an other but i dont want to do it.

How can i catch same event of view from multiple activities with a single event code ?
 
You can declare a class that takes care of the common layout elements, by code or loading it. Each activity declares an instance of that class and initializes it passing itself or a Panel (as a parameter) where the layout will be loaded.
The common elements and events will be declared in that class. Then each activity will have a class instance for that part of UI, and you will only need to write the event once, in the class

Should work...
 
Upvote 0
I am getting following error :

Code: Copy
v.Initialize(Me)
javac 1.8.0_111
src\b4a\example\classview\ikinci.java:346: error: incompatible types: Class<CAP#1> cannot be converted to BALayout
mostCurrent._v._initialize(null,mostCurrent.activityBA,(anywheresoftware.b4a.objects.ActivityWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.ActivityWrapper(), (anywheresoftware.b4a.BALayout)(ikinci.getObject())));
                                                                                                                                                                                                                                                ^
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
 
Upvote 0
Nevertheless you MUST catch the event in each an every activity, or it will not fire at all.
What you can do is to have a simple line inside every event to call a sub in a code module... Keep in mind, however, that a code module cannot interact with the GUI directly!
 
Upvote 0
You can declare a class that takes care of the common layout elements, by code or loading it. Each activity declares an instance of that class and initializes it passing itself or a Panel (as a parameter) where the layout will be loaded.
The common elements and events will be declared in that class. Then each activity will have a class instance for that part of UI, and you will only need to write the event once, in the class

Should work...
Basically creating a "Custom View" to handle all and each activity
 
Upvote 0
Ok. Its just runned. Cannot load layout to activity but in a panel. I just need to make one layout file more.

Thank you :D

Here is Codes

Class :

Code: Copy
Sub Class_Globals
    Dim a As Object
    Dim p As Panel
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (activity As Object, panel As Panel)
    a = activity
    p = panel
End Sub

public Sub loadLayout(layoutfile As String)
    p.LoadLayout(layoutfile)
End Sub


Sub Button1_Click
    Log("button 1 click")
End Sub


Sub Button2_Click
    CallSubDelayed(a,"Button2_Click")
End Sub


Main :

Code: Copy
Sub Process_Globals
End Sub

Sub Globals
    Dim v As viewClass
    Private container As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("container")
    v.Initialize(Me,container)
    v.loadLayout("1")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button2_Click
    StartActivity(ikinci)
End Sub

Activity 2 :

Code: Copy
Sub Process_Globals

End Sub

Sub Globals
    Dim v As viewClass
    Private container As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("container")
    v.Initialize(Me,container)
    v.loadLayout("2")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
Back
Top