Android Question Question on Activity Cycle and Home Button

ducphu

Active Member
Licensed User
Longtime User
My app has 2 activities, let say Activity1 and Activity2. I write the code for Activity1 first so in the manifest it is my Main Activity. I then realize that I want to have an activity that start before it, so I create Activity2. In the manifest I make changes so that Activity2 will be called first when my app is launched.
My question is, looking at the log, when my app is launched, "Activity2 Create" is not shown. After that, from Activity2, I press a button to call up Activity1. Now the log shows "Activity1 Create", "Activity1 Resume". So, why is the "Activity2 Create" info missing? Is it normal?

Secondly, if I'm in Activity1, and I press HOME button, if I'm not wrong, both activities will be paused? But, for my case, only Activity1 is paused. No log info on Activity2...After that, I immediately launch my app again. My app is still in memory so Activity1 is supposed to be un-paused and I supposed to see Activity1, but no, it starts with Activity2.

Thirdly, everytime Activity1 is called with StartActivity command, it starts with Activity_Create sub, not Activity_Resume (log shows Activity1 Create -> Activity1 Resume)

Can anyone explain what is happenning....

P/S below is full log and step be step how I get the log:

Installing file.
PackageAdded: package:b4a.example 'Launch App, App starts with Activity (2)
** Activity (1) Create, isFirst = true ** 'Press a btn in activity (2), with StartActivity(1)
** Activity (1) Resume **
** Activity (1) Pause, UserClosed = true ** 'Press BACK key
** Activity (2) Resume **
** Activity (2) Pause, UserClosed = false ** 'Press the btn in activity (2) again
** Activity (1) Create, isFirst = false **
** Activity (1) Resume **
** Activity (1) Pause, UserClosed = true ** 'Press BACK key
** Activity (2) Resume **
** Activity (2) Pause, UserClosed = false **
** Activity (1) Create, isFirst = false ** 'Press the btn in activity (2) again
** Activity (1) Resume **
** Activity (1) Pause, UserClosed = false ** 'Press HOME key
Killing previous instance (2). 'Launch the app again
** Activity (2) Create, isFirst = false **
** Activity (2) Resume **
** Activity (2) Pause, UserClosed = false **
 
Last edited:

ducphu

Active Member
Licensed User
Longtime User
Hi Erel,
For the first issue, to make it simple, I decide to swap the code from Activity1 and Activity2. So no more issue.
For second issue, I found this to be an android bug. Basically, after app is installed, there are 2 buttons "Open" and "Done". If user press Done and later open the app, everything is normal, i.e the app continue with the last paused activity. But if user select Open the app directly, then this bug happens, i.e the app will always restart itself and startup with root activity. You can test this bug using the "Two Activities" app found in Tutorial section. Step to generate the bug:
1. Install app, press "Open", App starts with Activity1(root)
2. Switch to Activity2 by pressing a button on Activity1, e.g StartActivity(2)
3. Press HOME KEY
4. Re-launch the app by clicking on app icon.

What you will see is the app shows Activity1 instead of Activity2.
More details can be found here:
http://stackoverflow.com/questions/...ton-but-only-the-first-time/16447508#16447508
https://code.google.com/p/android/issues/detail?id=38194

It seems that this bug was long ago but has not been fixed yet. Currently I'm using some workaround. In my root activity, I added
B4X:
Sub Globals
    Dim bug_fix As Boolean = False
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = False Then
        bug_fix = True
        Return
    End If
.....
End Sub
Sub Activity_Resume
    If bug_fix = True Then
        StartActivity(Activity2)
        Return
    End If
....
End Sub

Another weird thing is, if I just use
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = False Then
        StartActivity(Activity2)
        Return
    End If
.....
End Sub

It still calls up my root activity Activity_Resume and hence causing errors.
If you have better solution please help :)
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Hi Erel, I just discovered, this bus is not related to android but somehow due to B4A or B4a Bridge. Steps to generate bug:

1. Connect phone with B4A using B4A Bridge. Alt + 3 to compile and install app to phone.
2. Click open app after installation done. App shows Activity1 (root). Switch to Activity2 by pressing a button on Activity1, e.g StartActivity(2)
3. Press HOME KEY
4. Re-launch the app by clicking on app icon. What you will see is the app shows Activity1 instead of Activity2.

** Importance: All the above steps are performed while B4A Bridge is still connected and the app is in debug mode. If you stop debugging (i.e press the square button in B4A) before going to step 3. Everything is normal, the bug wont happen. AND, if you make the bug to happen, it will permanently stay there even if you stop debugging.

Just want to share :)
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
I think you didnt get want I mean. I can sure that when I restart the app, it's still in the memory. Let see 3 scenarios after the app is installed.
1. If the app is still connected to B4A for debugging and I press HOME, then relaunch the app by clicking on the app icon --> bug
2. Repeat compile and install the app. The app is still connected to B4A for debugging, press HOME key. This time I relaunch the app by using Recent Apps menu. -> NO bug -> so app is still in memory. Closed, and immediately relaunch using app icon --> bug again. Try again with Recent Apps menu --> NO bug.
3. Repeat compile and install the app. Open it, stop debugging --> the app automatically closed. Open again by app icon, press HOME, reopen.... everything is normal, no bug.

So, I can say that the bug happen when you press HOME key while the app is still in debug mode.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Here the log:

Installing file.
PackageAdded: package:anywheresoftware.b4a.samples.twoactivities
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (activity2) Create, isFirst = true **
** Activity (activity2) Resume **
** Activity (activity2) Pause, UserClosed = false **
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
 
Upvote 0
Top