Activity Stack woes

Kevin

Well-Known Member
Licensed User
Longtime User
I have an issue with my app that is difficult to reproduce. On rare occasions, when the user goes to exit the app ("back out") from the main screen, the app appears to restart itself. What I think is really happening is that there is more than one instance of the main activity in the activity stack. I've had the app appear to restart once (on several occasions) but a customer recently told me that one time he had to exit the app 3 times in a row to get it to actually exit.

I always use Activity.Finish when UserClosed = True in Activity_Pause but I think this problem pops up from the user randomly using the back key AND the Home key to leave the app. Naturally there is no way to detect the Home key (doesn't even trigger the Activity_KeyPress event) so I can't account for this.

In addition to how the user leaves the app, I think this problem is being compounded by the following:

1) The user can start the app from the main app shortcut.
2) The user can also start a specific activity (NOT the main activity) through a special home screen shortcut that starts that specific activity.
3) The user can also start/resume the app by pressing on a notification.
4) The user can also resume the app from the recent apps list.

Somehow the combination of how the user enters and exits the app seems to be causing more than one instance of the main activity to be in the activity stack.

Does anyone have any ideas on how to fix this? Is there a command in B4A to clear the app's activity stack? If not, would it be possible to add one? :D
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
2) The user can also start a specific activity (NOT the main activity) through a special home screen shortcut that starts that specific activity.
The main problems I saw were with the shortcuts. (Not completely happy with notifications, but they never pose a problem for me since I have only one main activity).

For the shortcuts add the following to your manifest:
B4X:
SetActivityAttribute(activityShortcutHandler,android:excludeFromRecents,"true")
SetActivityAttribute(activityShortcutHandler,android:noHistory,"true")
SetActivityAttribute(activityShortcutHandler,android:taskAffinity,"")

From the docs:
The affinity determines two things — the task that the activity is re-parented to (see the allowTaskReparenting attribute) and the task that will house the activity when it is launched with the FLAG_ACTIVITY_NEW_TASK flag.
By default, all activities in an application have the same affinity. You can set this attribute to group them differently, and even place activities defined in different applications within the same task. To specify that the activity does not have an affinity for any task, set it to an empty string.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I might give that a try if my problems continue. One question though: The shortcut is created and executed through an add-on app. Then the add-on executes the specific activity in the main app. So should I put that code in the manifest for the add-on app or the main app?

I also stumbled on this info from a Google search. With that in mind, for now I have tried the following which I think will force my app to only have one "task" at any given time, which I hope may fix it. There are also other options there (such as "SingleInstance") which may work as well.

B4X:
SetActivityAttribute(Main, android:launchMode, "singleTask")
SetActivityAttribute(Prefs, android:launchMode, "singleTask")
SetActivityAttribute(ArrangeLists, android:launchMode, "singleTask")
SetActivityAttribute(FavsGuide, android:launchMode, "singleTask")
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I might give that a try if my problems continue. One question though: The shortcut is created and executed through an add-on app. Then the add-on executes the specific activity in the main app. So should I put that code in the manifest for the add-on app or the main app?
You should put it in the Main app, on the activity that will be launched by the shortcut.

I know there are many of these launchmodes, I am not sure what is best for your app. You may have to experiment a bit on that.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
if suspecting multiple instances, i would create a simple file containing a number warning that my app is already running and probably exit immediately.
i find your observation very interesting, i have to take a closer look to some of my apps for such behavior. thank you!
 
Upvote 0
Top