Android Question Change OS Language without activity destroy.

Blueforcer

Well-Known Member
Licensed User
Longtime User
Hi everyone,

I'm working on an app running on a rooted Android 9 panel that's embedded in a control system for swimming pools. The user interacts solely with our app after boot, but they need to configure system settings like Wi-Fi, Ethernet, and time. To avoid reinventing the wheel by coding all these features from scratch, especially Ethernet (which isn't supported natively in Android), we open OS intents from our app to handle these settings.

The challenge comes when it comes to system language. We want the language of the OS menus to match the app's language. To achieve this, we use the app net.sanapeli.adbchangelanguage to change the system language via a shell command (e.g., am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language fr,en). This command is executed from our app whenever the user changes the language.

The problem is that executing this command seems to cause the app to restart, despite "FirstTime" being False. As our app needs to communicate continuously with bus devices, this restart disrupts its operation. I’ve tried placing the B4XPagesManager.initialize inside FirstTime, but it only results in a white screen.

B4X:
** Activity (main) Pause event (activity is not paused). **
** Activity (main) Create  **
** Activity (main) Resume **

Has anyone experienced a similar issue or found a way to change the system language without restarting the app? Any help would be greatly appreciated!

Thanks in advance!
 
Last edited:
Solution
I see. B4XPagesManager must be created every time that the activity is recreated. Otherwise you will not have any user interface. Usually it only happens once with B4XPages.

1. Handling onConfigurationChanged event: https://www.b4x.com/android/forum/threads/onconfigurationchanged.64333/post-407263
2. Add to manifest editor:
B4X:
SetActivityAttribute(Main, "android:configChanges", "locale|layoutDirection")

Not tested. This is expected to prevent the activity from being restarted when the locale changes.

DonManfred

Expert
Licensed User
Longtime User
I guess it is the Androidsystem which forces all apps to restart when the language is changed.
So you should expect this to happen.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem is that executing this command seems to cause the app to restart, despite "FirstTime" being False. As our app needs to communicate continuously with bus devices, this restart disrupts its operation. I’ve tried placing the B4XPagesManager.initialize inside FirstTime, but it only results in a white screen.
Based on this description, the app isn't restarted. It is the activity that is restarted. Never implement communication code in activity. Best to switch to B4XPages and if not possible, implement it in a service (starter service is good for this).
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
Based on this description, the app isn't restarted. It is the activity that is restarted. Never implement communication code in activity. Best to switch to B4XPages and if not possible, implement it in a service (starter service is good for this).
It is B4XPages :

I’ve tried placing the B4XPagesManager.initialize inside FirstTime, but it only results in a white screen.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see. B4XPagesManager must be created every time that the activity is recreated. Otherwise you will not have any user interface. Usually it only happens once with B4XPages.

1. Handling onConfigurationChanged event: https://www.b4x.com/android/forum/threads/onconfigurationchanged.64333/post-407263
2. Add to manifest editor:
B4X:
SetActivityAttribute(Main, "android:configChanges", "locale|layoutDirection")

Not tested. This is expected to prevent the activity from being restarted when the locale changes.
 
Last edited:
Upvote 0
Solution

Blueforcer

Well-Known Member
Licensed User
Longtime User
B4X:
SetActivityAttribute(Main, "android:configChanges", "locale|layoutDirection")
The Manifest entry seems to solve my proplem! Handling the onConfigurationChanged wasnt necessary in my case.
Thank you very much!
 
Last edited:
Upvote 0
Top