Android Question Best solution for Android? Activity/Layout?

Roger C

Active Member
Licensed User
Longtime User
Hi,

This is a general question, what approach is best for Android?
Is it to use an Activity for each screen or to use one Activity and change screens through Layouts?

I've read the tutorial about the four variants and the conclusion is, it's up to the project and you as programmer to choose what works best...

But I'm curious what solution you use in your apps. Which solution do you prefer? Also in regards to the orientation changes. Which is easiest to handle?

I thought about using the Sliding Menu Wrapper and have one activity and change layouts to keep the menus intact but run into problems with addressing the items in each layout. I had to keep track of which layout was used at the moment.
Now I'm thinking of going back to many Activities instead and remake the Sliding menu in each activity...

Like to hear your input in this topic.
 

Roger C

Active Member
Licensed User
Longtime User
Thanks for your fast replies, as always.

Handling code is ok after 25-30 years in VB6 and 2010, but I'm not so used to Android yet though...

So Erel, I guess you mean that in the Activity, in sub Main, I declear all used buttons, listviews... in all Layouts, then I catch all events from the different items in all layouts in Main. Then the event subroutins (btnDate_Click for example) in Main will start a sub in the Codemodule that belongs to that Layout.

Another problem then; I want to change the displayed information in the active Layout, ie change Button text, Listviews...
Do I adress them from the Codemodule with Main.btnDate.text for example?

Maybe it's good practice to name all View-items in the designer with the Layoutname too, then it's easier to see what Layout they belong to because it will be a lot of _Click subroutines in Main... Group them together with Regions too...
 
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
But that doesn't work of course since the viewitems in the layout displayed in Main isn't decleared in Process_Globals...

How do I access them from the Codemodule??????
Main.btnDate doesn't exist in the codemodule...

Do I have to write millions of subs in Main that I call from the Codemodule passing the text that I want changed in the button, listview???
And another million subs to retrieve the information from the layout too....

And now... Subs in Activities are not public either...
So I need to create a custom type for every sub I need to call, fill it with my variables (yes, I would need 5-8 passed sometimes) then use CallSub2 with my type...
And I will also have to check that the activity really is active and not hidden by another app by the user...

I think I will abandon the slidingmenus and use a lot of activities instead. This method will increase my code with 200%...

Does anyone have a better solution than my last?
Maybe I should use many activities instead and setup a slidingmenu for each of them... Question is how does it look when changing activity?
I want to use THIS slidingmenu...

I could use global variables or KeyValueStore to send information to the Codemodule but I'd still need to send things back to the layout.
Maybe I should put all my code in Main instead, the communication between Codemodule and the Layout gets too complicated.
 
Last edited:
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
Ok... now I'm swimming in deep water I think. Handling this much code from scratch without really knowing how this works is not so easy... I'm a guy and as a guy I like to press buttons and make things happen, in the very last moment of despair I turn to the manual.

Been searching the forum for help here but not really sure.

How do I load a layout from the class? Haven't found any extensive example how to do that.

I found this question 'Handle events in a class' where the layouts are loaded in Main then the events are trapped in the class by Reflectionlibrary but I don't think that is what you mean?

Do I have to create a panel from the class and then load the layout into the panel? Like here LoadLayout in a Class produces "...has stopped" error.
Do I have to initiate all views in the class? Because in the last example they are initiated in Main...
How do I handle rotations? Since the activity (Main) will be resumed do I have to initiate a new class and all views again?

Feels like I will be spending weeks just getting the look correct and working...
What I like to do is have a sliding menu from left and one from right. Couldn't I just use jfeinstein10 SlidingMenu library in every activity, set it up in activity create or will that not work? Then I can handle everything in their own activity.
 
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
I've found this code in this question:

Main
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim L As LayoutTest
    L.Initialize(Activity)
End Sub

Class
B4X:
'Class module
Sub Class_Globals
    Private DrawBtn As Button
    Private Panel1 As Panel
    Private ShowBtn As Button
    Private ToggleButton1 As ToggleButton
    Private ToggleButton2 As ToggleButton
    Private EditText1 As EditText
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Act As Activity)
  
    DrawBtn.Initialize("DrawBtn")
    Panel1.Initialize("Panel1")
    ShowBtn.Initialize("ShowBtn")
    ToggleButton1.Initialize("ToggleButton1")
    ToggleButton2.Initialize("ToggleButton2")
    EditText1.Initialize("EditText1")

    Log(Act.LoadLayout("panel.bal"))
    For i = 0 To Act.NumberOfViews-1
        Log(Act.GetView(i))
    Next

End Sub
Private Sub DrawBtn_Click
    Log("Clicked")
End Sub
Sub EditText1_EnterPressed
    Log("ET"&EditText1.Text)
End Sub

This works and the events are catched in the _Click and _EnterPressed.

Question:
How do I change what layout to show?
Could I use this code in Main to remove all views and then initiate another class and show another layout?

B4X:
Sub ActivityRemoveViews
    Dim i As Int
    For i=Activity.NumberOfViews-1 To 0 Step -1
        Activity.RemoveViewAt(i)
    Next
End Sub

What happens when the orientation is changed? Should I use above code and then CallSub in the Class to load the vertical layout? But first save all entered information in the layout and reload it in the vertical layout?

If the user choose to view the first layout again, I guess I have to test IsInitialized and then either initialize a new class or callsub to class from Main, to just load the layout?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…