Android Question [Solved]Wrong activity resumed

noeleon

Active Member
Licensed User
If my app is in the background and I go back to it by clicking the launcher icon sometimes the Main activity is resumed instead of the last visible activity. Any possible reasons for this behavior?

This is how the activity is started,

in Main:
Clicking a listview item starts PlayerActivity via CallSubDelayed
CallSubDelayed(PlayerActivity, "CallService")

in PlayerActivity:
B4X:
Sub CallService
    StartService(svcPlayer)
End Sub

BTW it doesn't have a Starter service.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
maybe android decided to kill the app in the meantime because of in background. Usually Main is started in this case.
 
Upvote 0

noeleon

Active Member
Licensed User
It turns out Android kills the Main activity but PlayerActivity is not killed. This rarely happens but when it does it keeps happening everytime I resume the app. It only goes back to normal if I close the app.

Here's the log:

Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
=== Wrong resume ===
** Activity (main) Pause, UserClosed = false **
** Activity (PlayerActivity) Resume **


in Main:
B4X:
Sub Activity_Resume
    If PlayerActivity.PlayerIsOpen Then
        log("=== Wrong resume ===")
        StartActivity(PlayerActivity)
    End If
End Sub
 
Upvote 0

noeleon

Active Member
Licensed User
Solved my problem by moving all contents of Main to a new activity called OhMaine. This way if by Android's divine wisdom it decides to kill Main and create a new instance I just call Activity.finish so it will resume the last paused activity.

This is now all that's left in main
B4X:
Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
End Sub

Sub Activity_Resume
    If OhMaine.isRunning Then
        Activity.finish
    Else
        StartActivity(OhMaine)
    End If
    Activity.Finish
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

This was a real pain in the rear because I could never predict exactly when it will happen, only that it sometimes happens if the launcher icon is clicked -- not if resumed from the recents list or through the notification that's tied to an activity (not main).

Also, I found the sure-fire way to trigger this behavior is by running the app, open an activity (not main) then install/update via apk.
(works in Android 6)
 
Last edited:
Upvote 0
Top