Android Question How to handle "Killing previous instance (Main)"

Arf

Well-Known Member
Licensed User
Longtime User
I'm starting a new thread on this, as I am now more clear on the problem I was having and this thread title is much more apt.

The historical thread is here: https://www.b4x.com/android/forum/posts/493849/
https://www.b4x.com/android/forum/posts/493849/
I now realise that when starting an app that is in the background by clicking the app icon, SOMETIMES the last activity is simply resumed, but other times the OS thinks that it's starting a second instance, and kills the first instance. When this happens, I get "Killing previous instance (Main)" in the log window, and I run into problems.

The problems:
The re-created Main activity runs, then if I click on an icon to enter another activity (one that was active in the session prior to going into the background and me clicking the app icon to return to it), then that activity does start up, but totally misbehaves. For example if I click the back button to return to the Main activity, this fails. So I think something has gone wrong with the stack, control is trying to go from my activity back to the OTHER main (that the OS killed).

I have no idea how to handle this.
I thought if I could somehow detect (in main) that the previous instance has been killed, maybe I could somehow destroy the instances of the other paused activities, so they'll be newly created.
Or somehow end this new activity and revert back to the old one before it gets killed.
 

Arf

Well-Known Member
Licensed User
Longtime User
Yes, copied and pasted into Patients with the other Activity_Keypress commented out.
It seems to work OK in release, but when coming to the foreground it's going directly to the Patients activity, so I think the 'Killing Instance (Main)' thing is not happening (for now, it comes and goes).
In debug, I can see it is doing 'Killing Instance (Main)' and after that, that back button is not intercepted.
I've put a breakpoint on it, the picture shows no BP hit when I press back button (I took the screenshot 5 seconds after pressig BACK). Everything just locks up for a few seconds, nothing arrives in the Log and after a minute or 2 android kills the app.

The only strategy that seems to be working for me is to add "SetActivityAttribute(main, android:launchMode, "singleInstance")" to the manifest (this seems to stop the 'Killing Instance(Main)' thing from happening, and adding this code to track what activity was active, so I can restore the right activity from within Main after the app icon is pressed
B4X:
       If Global.LastActivity = "patients" Then
        If IsPaused("Patients") = True Then
            StartActivity(Patients)
        End If
    Else If Global.LastActivity = "calib" Then
        If IsPaused("Calib") = True Then
            StartActivity(Calib)
        End If
    Else If ...

If there is no inherent danger in adding that line to the manifest, I think I'll just have to proceed with that. I'll be testing it with all my other tablets and different OS versions this afternoon, hopefully it works solidly on all of them.
 

Attachments

  • Clipboard01.jpg
    Clipboard01.jpg
    389.3 KB · Views: 243
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It seems to work OK in release, but when coming to the foreground it's going directly to the Patients activity, so I think the 'Killing Instance (Main)' thing is not happening (for now, it comes and goes).
This is the expected behavior. The user uses the recent apps list to return to previous tasks. When the user clicks on the app icon it start a new task.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
That may be true, but my software testers don't know that you shouldn't revert back to an app by clicking the icon.
They create test scripts based on what operators are most likely going to do, and upon what is possible to do, regardless of 'shoulds'.
Many apps do behave in the same way (icon click restores the app instead of restarts it).

So they stumbled upon it, it became a test script because it causes issues, and thus it will forever remain a test script regardless of what I say.
We have to develop/test to ISO13485 and various medical standards, part of which means the testers and developers cannot be the same people and that I shouldn't be explaining them around issues, only fixing issues they encounter.

I wouldn't actually mind if by clicking the icon while the app was backgrounded, a new instance was started and the operator had to start from the beginning rather than where they were, but I am clearly having stack problems under those circumstances. Why doesn't android also kill all the other Activities on the stack? It would work properly then I guess.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Mmm, I didn't like the extra time it takes to load layouts with the
SetActivityAttribute(main, android:launchMode, "singleInstance") manifest addition.

I removed that and tried some other stuff, and the addition of this
SetActivityAttribute(main, android:clearTaskOnLaunch, "true")

Seems to work better, and I don't get the 'Killing Previous Instance (Main)' thing happening any more, and the fast switching between layouts is back. So this looks like the best solution to me so far, any gremlins introduced by adding that setting are not known to me yet!
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Just to confirm, I've checked app operation on all my devices now, and the addition of "SetActivityAttribute(main, android:clearTaskOnLaunch, "true")" into the manifest seems to have been the best solution in sorting out the problems I was having.
 
Upvote 0
Top