Android Question App start and exit, OK to use ExitApplication?

Arf

Well-Known Member
Licensed User
Longtime User
I had some troubles when an app went into the background, and I press the app icon to get it up again. Min was being destroyed and recreated, and odd behaviour ensued. I fixed it with a suggestion from Avansy, by ending main on entry if FirstTime is false:
B4X:
Sub Activity_Create(FirstTime As Boolean)
If FirstTime = False Then
Activity.Finish
Else
do something....
End If
End Sub

Then I found I could not start my app once I had killed it completely in the task manager, FirstTime was still true when starting the app. I guess maybe my service module was still running beccause I don't stop it. So, I modified my app closing by adding an ExitApplication, like this:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
        CallSub2(Unit_Comms, "KillUnit", "Main")
        Awake.ReleaseKeepAlive
        ExitApplication
    End If
End Sub

Seems to work well now, but I just wanted to ask if my strategy seemed sound, because I recalll hearing that ExitApplication should not be used if possible. Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. No, you shouldn't call ExitApplication. The OS will treat your app as it if crashed.
2. And you shouldn't call Activity.Finish in Activity_Create. Your app will fail if the user clicks on the home button and then back to your app.

I had some troubles when an app went into the background, and I press the app icon to get it up again
What is the problem?
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Thanks, I think I've solved it now. I removed both 1 and 2 and made another change.

I had odd behaviour when clicking on the app icon to start the app, when the app was in the background with my "PerformTest" activity active. The normal entry point is a main screen, on which you can click an icon to start the PerformTest activity. So when in the background and clicking the icon, it start from the main activity again instead of just hauling up the paused PerformTest activity.

I had a bit of code in main that tracks if the PerformTest activity is what the operator was on, and this bit of code in main starts the PerformTest activity if this was the case.

B4X:
Sub Activity_Resume
    If isInPerformTest = True Then
        StartActivity(PerformTest)
    End If
End Sub

It worked in practice, but when I click any button on the PerformTest screen after tht situation, my app would fly off into the background instantly and behave oddly. That's why I made those changes.

Anyway, I've undone those changes now, and modified that bit of code in main to this instead:
B4X:
Sub Activity_Resume
    If isInPerformTest = True Then
        If IsPaused("PerformTest") = True Then
            StartActivity(PerformTest)
        End If
    End If
End Sub

And all seems good! Just going back to test on droid 4.4.4 and 5 now. So I guess it was starting a second instance, that was my problem.

It always makes me smile, so often a problem seems so dumbfoundingly complex that I run around in cricles for days like a headless chicken trying to solve it, and when I eventually stumble across the solution, its usually so obvious.

Thanks for your guidance, it is very much appreciated.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Ah man, the problem is still there, I'm banging my head against a wall.

so I think the fundamental question I need to answer is this:

Click app icon for first time, main activity starts. If you then click the PerformTest button, a 2nd activity is started - PerformTest.
B4X:
** Activity (main) Create, isFirst = true **
starting main
** Activity (main) Resume **
** Service (unit_comms) Create **
** Service (unit_comms) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (performtest) Create, isFirst = true **
** Activity (performtest) Resume **
Now put the app in the background by clicking the android circle button
B4X:
** Activity (performtest) Pause, UserClosed = false **

Desktop is shown. Now click the app label to get back onto the app. This kills the previous instance, whatever that means, and starts a new instance of main.
B4X:
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
starting main
WakeLock already held.
** Activity (main) Resume **

In main, my code determines that the last activity the operator was in was PerformTest, so it starts that activity again (if it is paused or null), the code looks like this:
B4X:
Sub Activity_Resume
  If isInPerformTest = True Then
    IfIsPaused("PerformTest") = True Then
        StartActivity(PerformTest)
    EndIf
  EndIf
End Sub

This is what happens in the log:
B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (performtest) Resume **

PerformTest activity seems to load up OK, now I click a button on PerformTest screen, and instantly my app disappears and I'm looking at the desktop.
The log only shows this:
B4X:
** Activity (forcedtest) Pause, UserClosed = false **

If I put a breakpoint on the CLICK function of that button, I do end up hitting the breakpoint, but the Activity has disappeared off the screen by that stage. Hence, I can't seem to step through code to isolate at which point the activity flies off.

So, getting back to what my actual question is - how do I return to my PerformTest activity under this set of circumstances, assuming that me calling StartActivity(PerformTest) from the killed and resurrected MAIN activty after app icon is clicked?
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
And now its behaving again, but I can see in the logs that when repeating the exact same steps, clicking the app icon does this:
B4X:
** Activity (performtest) Resume **

And not what it was doing earlier:
B4X:
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
starting main
WakeLock already held.
** Activity (main) Resume **

Why?!?!?!? Under what circumstances does it do to the 'kill' thing? I am so confused.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Under what circumstances does it do to the 'kill' thing?
This happens when the OS sends an intent that creates a new activity. The old activity instance is then killed.

However this is not the normal behavior. The app should start from the second activity if the user presses on the home button while it was visible.
Are you calling Activity.Finish in the second activity?
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
No, I don't finish the 2nd activity, I am just putting it into the background by hitting the HOME button (for example to go check an email), and then after done with the emails I'm clicking on the app icon to hopefully go back to where I was.
 
Upvote 0
Top