Android Question B4XPages - SQLite Create Table Displays Data Only on the First Run

Mahares

Expert
Licensed User
Longtime User
I use runtimepermission, create a SQLite table and display data in B4XMainPage. All works well the first run. I run the app in debug or release. The first run it displays the data as expected in the logs. I exit the app with the back key and run it a second time. No table data is displayed in the logs the 2nd run.
After clicking the back key to exit the first time:, the log is:
** Activity (main) Pause event (activity is not paused). **

After I run the 2nd time here is the log, but no data is displayed:
** Activity (main) Resume **
 

Mahares

Expert
Licensed User
Longtime User
You misunderstood my answer. You are not "running the app again".

Here is the code in Main:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Sub Activity_Resume
    B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    B4XPages.Delegate.Activity_Pause
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Here is the code in Starter Service:
B4X:
Sub Process_Globals
    Public MyFolder As String    = "test"
    Public DBFileName As String = "test.db"
    Public DBFilePath As String
    Public SQL1 As SQL
    Public rp As RuntimePermissions
End Sub

Sub Service_Create
    If File.ExternalWritable Then
        DBFilePath = File.DirRootExternal & "/" & MyFolder
    Else
        DBFilePath = File.DirInternal & "/" & MyFolder
    End If
    File.MakeDir(DBFilePath,"")
   
End Sub

Here is the relevant code in B4XMainPage:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root=Root1
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for B4XPage_PermissionResult(Permision As String, Result As Boolean)
    If Result = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else
        If Starter.SQL1.IsInitialized =False Then
            Starter.SQL1.Initialize(Starter.DBFilePath,Starter.DBFileName,True)
        End If
        Log("granted")
    End If
    Log("hi")
    CreateTable

    DisplayData
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Do you want to Exit ?", "Exit", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
'        Starter.SQL1.Close
        Return True
    End If
    Return False
End Sub
If that is not sufficient for you to determine the problem, I can zip and export.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I don't think that there is a problem
I will play with it again and if the problem continues, I will switch to a different tablet. I am telling you, there is a problem somewhere. You need to understand that what is simple to you is not to us. I have no problem running the application without the use of B4XPages, but that is not the way to learn by avoiding it. B4Xpages is not as simple as you make it to be.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tested your project and it works OK.
To see what happens in Release mode I added a ListView which is filled in the DisplayData routine.
It works OK in both Debug and Release mode. Also when commenting CreateTable.

Attached the test project.
 

Attachments

  • SQLiteWithB4XPages.zip
    10.3 KB · Views: 184
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please read carefully ?
1. B4XPage_Created is only fired when the page is created. It will only fire once.
2. B4XPages are never killed. Only removed from the stack.
3. The process wasn't killed so when you clicked on the icon the page became visible but it wasn't created again.
4. You can handle B4XPage_Appear if you like (though there is no reason to do it here).
5. Better not to use the starter service unless you must.
6. Better to always use File.DirInternal and avoid requesting the runtime permission.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I tested your project and it works OK.
Please read carefully ?
Thank you both for testing.
@klaus: If you comment the listview code and run the logs code line, the page appears on subsequent app runs, but it is empty, it does not show the logs. That is what I was referring to in my previous posts. Therefore, not seeing anything in the logs spooked me. Having ONLY logs in the B4XMainPage B4XPage_Created sub in not a good idea. By virtue of you adding the listview, the data shows up in the listview.

@Erel: Here is what I mean by 2nd run:
1. Compile and run in Release. The logs appear.
2. Exit the app with the back key code,
3. Now, go to the app's icon on the tablet and click it to run it again.
4. The logs do not show anything.
But now, after your clarification in post #13, it proves why the logs do not show anything in subsequent runs.
For testing purposes, I am playing with the Starter Service. I like using direxternal with rtp,, especially for testing purposes.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
That's the reason why I added the ListView
Do we agree that, we should not expect to see the logs in the B4XMainPage B4XPage_Created sub on subsequent app runs because the page is not recreated. I hope Monsieur Erel mentions it somewhere in the tutorial, because I guarantee you that some other developers will run into it like I did as B4XPages gains popularity and get discombobulated like I did.
I hope you do not get in trouble with Erel for using a Listview instead of xClv. Just kidding. Note that I do not use any emojis in my posts.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Check point #3: https://www.b4x.com/android/forum/t...hat-exactly-does-it-solve.119078/#post-744771

The three most important things regarding the B4XPage classes are:

  1. The page classes are 100% regular classes. They don't have a special life cycle and you can do whatever you like with them. There are some B4XPages events but they don't affect the state of the class itself. This is not the case with activity modules.
  2. The page classes are never paused. Nothing special happens when a page is no longer visible or when the app moves to the background. Eventually of course, the whole process will be killed when the app is in the background.
  3. The page classes are never destroyed separately. The class global variables and views state will never be reset (until the process is killed).
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…