Android Question Restart B4X App by Buttom

mw71

Active Member
Licensed User
Longtime User
I have an app (B4X Pages) with 3 pages. I want to be able to close them by the user and restart them automatically (by pressing a button).

in another B4X Pages App I have solved it with the following code in the main module:
B4X:
    Activity.Finish
    StartActivity(Me)
In this app it is triggered by rotating the device (detect in the Main Code Modul), but I can't get it solved to trigger this by a button.
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
Closing a B4Xpage simply shows the next page in the showPage stack. It is not destroyed, so can be shown again.

Where is the button that shows the pages again?
If it is in the B4XMainPage instance then you can simply show whatever page you want.

Can you explain the situation in more detail?
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
Hi

the reason is that I want to change the orientation of the app (regardless of the orientation and rotation of the device). This is (currently) only done when the app is startet.
The orientation is cached in a file. This works fine so far.

The button/query is on a 2nd B4XPage. Accessing the elements of the main B4XPage is no problem, but I haven't found a way to access the main module and thus the activity.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
There is still too little info for me to fully understand what you want to do.
For example I don't know what you mean by "The orientation is cached in a file."

What I get so far is (assuming that the starting orientation is portrait):
1. You have 3 pages
2. The second page has a button that when pressed closes all pages and restarts the Activity
3. On restart the orientation should be landscape, but the app should continue where you left off

Am I right?

It is possible to put the desired orientation in an external file and read it upon activity start.
Then set it before running pm.Initialize

Below is how I usually modify the Android B4XPages template for Main
Replace line #28 with a poll of the external file holding the desired orientation.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified          'this will cause a warning in logs - but ignore it - it is needed
    #CanInstallToExternalStorage: False
#End Region

Sub Activity_Create(FirstTime As Boolean)
    Dim ph As Phone   'include Phone library
'the next line will set the orientation to how the user was holding the phone when the App started
    If Activity.width < Activity.Height Then ph.SetScreenOrientation(7) Else ph.SetScreenOrientation(6)
 'now the B4XPages context has a fixed orientation as required       
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub

'Template version: B4A-1.01
#Region Delegates
 
Upvote 0
Top