Android Question How to bring my app to foreground no matter what activity was last displayed

JohnC

Expert
Licensed User
Longtime User
I have an app with a few different activities, lets say A,B,C,D.

One feature I am adding is an overlay launch icon/button using the overlay lib from this great toolkit (https://www.b4x.com/android/forum/threads/probundle-chargeable.58754/#content). This allows my app to display a floating icon on every screen of the phone even if my app is not in the foreground, and it will allow the user to jump right back to my app from anywhere on the phone.

OK, so when the user runs the app, A (main) is displayed. But then the user might invoke some code that displays activity B using StartActivity("B"). But now lets say the user presses the home button and the focus is switched to the home screen. The B activity is now paused as expected.

Then, lets say the user clicks on my floating icon because they want to go back to using my app. So, the current code I have behind the floating icon attempts to run my app this way (which I was hoping would just bring my app to the foreground):

B4X:
Dim Intent1 As Intent
Dim pm As PackageManager
    
Intent1 = pm.GetApplicationIntent (myapps.package.name)
StartActivity (Intent1)

But, it will cause this to happen in the event log:

B4X:
Killing previous instance (main).
** Activity (main) Create, isFirst = false **

But this kill/create of the A activity (even though the B activity was the last shown activity before the user switched to the home screen) causes all sorts of problems because the code in the A (main) is be executed again unexpectedly, causing errors.

I was thinking of just using StartActvity("B") behind the floating icon to bring my app to the foreground, but then I would have to keep track of which activity was last shown so I would know which one to "start" because the user might have had another activity opened when they switched away from my app. But that would make everything messy to manage and be prone to surprise issues if I add new activities to the project and forget about adding code to do this in it.

So, what code do I need to use in the floating icon "click" routine that will make my app come to the foreground and display the last shown activity without causing the A activity to be killed/recreated?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
But this kill/create of the A activity (even though the B activity was the last shown activity before the user switched to the home screen) causes all sorts of problems because the code in the A (main) is be executed again unexpectedly, causing errors.
Restarting Activity A shouldn't cause errors. You should use the starter service to manage global resources.

This code explicitly starts the main activity:
B4X:
Intent1 = pm.GetApplicationIntent (myapps.package.name)
StartActivity (Intent1)
You will need to track the current activity if you want to start the correct one from your code.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thanks for the info Erel, then I'll probably have to keep track of the last activity shown and start it when I want to bring my app to the foreground.

That's probably why I had that "singleInstance" in the manifest to prevent the kill/recreate.

The problems I am having with A reloading is that A hosts' the Dynamic Grid (https://www.b4x.com/android/forum/threads/msdynamicgridview-library.49331/#content) which has that crashing problem I posted a few months back where events are allowed to be invoked even when the control is not ready/visible, and events do get triggered when A is loaded again, and I get crashes because A is not visible.
 
Upvote 0
Top