Android Question Confirmation Code-Open/Close Panel/Activities

daniedb

Active Member
Licensed User
Longtime User
HI Guys
As you all know I'm a complete newbie in the Android/B4A Environment
Although I've done a few interesting and complicated Apps, I just want to know if my approach is correct, or Completely wrong.

This is how I open/close all my Panels/Activities

B4X:
Sub Globals
Dim p1, p2 As Panel
' *** Main Activity
Dim MainActivity_CloseBtn_click as Button
Dim Panel1_OpenBtn_click as Button
Dim Panel2_OpenBtn_click as Button
' *** Panel 1
Dim panel1_CloseBtn_click as Button
' *** Panel2
Dim panel2_CloseBtn_click as Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")

    p1.Initialize("")        
   p1.LoadLayout("Panel1")    
   Activity.AddView(p1,0,0,100%x,100%y)
   p1.Visible=False

    p2.Initialize("")        
   p2.LoadLayout("Panel2")    
   Activity.AddView(p1,0,0,100%x,100%y)
   p2.Visible=False

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
'*** Main Activity Panel
Sub Panel1_OpenBtn_click
  p1.visible=True
End
Sub Panel2_OpenBtn_click
  p2.visible=True
End
Sub MainActivity_CloseBtn_click
  Activity.Finish
End Sub

'*** Panel 1 Activities
Sub panel1_CloseBtn_click
  p1.visible=False
End Sub

'*** Panel 2 Activities
Sub panel2_CloseBtn_click
  p2.visible=False
End Sub

Is this the correct way of doing it? , or how do your Guru's do this?

Any helpful links/advice would be appreciated.

Thanks, Appreciate
Danie
 

abhishek007p

Active Member
Licensed User
Longtime User
For different screen i use different activities. I will only use that code (your code) when using TabHost.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I think you will find the recommended method is to use multiple activities for different screens .. similar in fashion to how you would design a program using VB and multiple "Forms". one reason for this approach is to make the associated code more manageable ..

That being said I believe the final decision how you proceed is yours alone ,obviously the complexity / size of the app will be a factor.

I have numerous apps on the go .. and all have a slightly different approach due to various levels of complexity and how much data is reqiuired/ displayed back to the user in different methods and styles.

Some apps with Single activities with multiple panels housing many child views, hidden and visible as required (similar to your example above)'
Others , single activity using multiple layouts .. using @ TabHost .. and @ TabHostExtras .. as abhishek007p stated ..
Others utilizing multiple activities hosting minimal amount of views each.
All the apps work well in there own way and I am happy with the look and feel and how the app behaves.

as to your request for different activities examples ... see Chapter 13.2 Program with 3 Activities in the @ The Beginners Guide ...
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
You may also have a look at this thread: Different examples with 2 layouts.
I agree fully with mangojack.
Android was origanlly written to work with Activites. The they introduced TabHost which works with Panels in the same activity.
The new layouts with the StdActionBar are also Panels in the same activity. So event Google has different approches.
The management with activities is simpler than with panels. When the program gets paused and reactivated it shows the last displayed activity. If you have one activity and panels it's up to you to manage which was the last displayed panel.
Then as mangojack already mentioned, it depends also on your 'feeling' and on the type of programs.
I had written a program for tablets only, then I wanted it also on phones so I wrote an independant version for phones. But maintaining two programs which have most of their code the same is not efficient. So I merged both with different layout files for phones and tablets working with panels in one activity. On the phones only one panels is visible at the same time but on the tablets two panels are visible at the same time. This couldn't be done easily with different activities.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Hi daniel,

I've been to that road initialy. A big app with lots of panels, showing and hiding..It has become a nightmare to manage the code.

So I've restructured everything to spread the code across several activities and the code became much easier to manage and mantain and now when I start something I always plan very carrefully the way to use multiple activities, as everyone above said.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Off topic but ...
nobody noticed anything?

B4X:
Sub Globals
Dim p1, p2 As Panel
' *** Main Activity
Dim MainActivity_CloseBtn_click as Button
Dim Panel1_OpenBtn_click as Button
Dim Panel2_OpenBtn_click as Button
' *** Panel 1
Dim panel1_CloseBtn_click as Button
' *** Panel2
Dim panel2_CloseBtn_click as Button

End Sub


A good convention is naming the views using three letters for the type of view.
btnOpen (Button)
edtLastName (EditText)
spnProducts (Spinner)
...
In addition, "_CLICK" is part of the signature of the event related to the view.
 
Last edited:
Upvote 0

TheMightySwe

Active Member
Licensed User
Longtime User
I found a BIG problem

B4X:
 p1.Initialize("")       
   p1.LoadLayout("Panel1")   
   Activity.AddView(p1,0,0,100%x,100%y)
   p1.Visible=False

    p2.Initialize("")       
   p2.LoadLayout("Panel2")   
   Activity.AddView(p1,0,0,100%x,100%y) ' <----- You must add panel p2 here, not p1 again.
   p2.Visible=False
 
Upvote 0
Top