Android Question [Solved] Strange behavior with Activity.Finish

Creideiki

Active Member
Licensed User
Hi there,

I created a little test app with B4A 10.7.
When I start it, I get an activity with a button. When I press it, I go to the second activity:
Activity 1:
Sub Button1_Click
    StartActivity("Activ2")
End Sub
When I press "back" there, I go back to the first one:
Activity 2:
Sub btBack_Click
    Activity.Finish
End Sub
So far, so good - that's what I want.

When I press the home button in the second activity, I get to the home screen. Now I restart the app and I get to the first activity. I press the button and get to the second activity.

When I now press the "back" button or the system back key, I don't get back to the first activity, but I am on the home screen! That's not what I expect.

What happens here?
 

Attachments

  • testApp.zip
    10.6 KB · Views: 29

Creideiki

Active Member
Licensed User
Well... there would be 17 Activities, 7 Services and about 20 other classes to be ported. Sorry, that would cost much time.
 
Upvote 0

Creideiki

Active Member
Licensed User
Hm... since its a quite linear setting that would be possible. I think I had to catch the back button, but that would be possible either.

But - why does that happen? Why do I have to make workarounds to the normal mechanism?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
It doesn't happen here. When I tap on the icon, after I clicked on the home button while Activity2 was in the foreground, I'm taken back to Activity2.
B4XPages will solve all of this and will also make the navigation UX better as it will show the useful "up" indicator.

I guess that on your device the second tap starts the same app and treats it as a new task. Later when the second activity, which was already in the stack, is started, it doesn't behave as expected.

The logs look like this:

** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Activity Create
** Activity (main) Resume **
Button clicked.
** Activity (main) Pause, UserClosed = false **
** Activity (activ2) Create, isFirst = true **
** Activity (activ2) Resume **
** Activity (activ2) Pause, UserClosed = false ** <--- home button
** Activity (activ2) Resume ** <--- icon tap
** Activity (activ2) Pause, UserClosed = true **
** Activity (main) Resume **
Button clicked.
** Activity (main) Pause, UserClosed = false **
** Activity (activ2) Create, isFirst = false **
** Activity (activ2) Resume **
 
Upvote 0

Creideiki

Active Member
Licensed User
OK... here's mine:
Logfile:
Logger verbunden mit:  LENOVO Lenovo TB-7304F
--------- beginning of main
** Activity (main) Resume **
Button clicked.
** Activity (main) Pause, UserClosed = false **
** Activity (activ2) Create, isFirst = false **
** Activity (activ2) Resume **
** Activity (activ2) Pause, UserClosed = true **
** Activity (main) Resume **
Button clicked.
** Activity (main) Pause, UserClosed = false **
** Activity (activ2) Create, isFirst = false **
** Activity (activ2) Resume **
** Activity (activ2) Pause, UserClosed = false ** <-- Home-Button
Killing previous instance (main).  <-- *** after tapping the icon
** Activity (main) Create, isFirst = false **
Activity Create
** Activity (main) Resume **
Button clicked.
** Activity (main) Pause, UserClosed = false **
** Activity (activ2) Resume **
** Activity (activ2) Pause, UserClosed = true **

The "Killing previous instance (main)." is the line that confuses me.

BTW: The tablet uses Android 7.
 
Upvote 0

Creideiki

Active Member
Licensed User
Thanks for the link. I wasn't aware there are such possibilities...
But: launchMode standard should be the default, as I read in <activity>. In my opinion that's not the behavior I see in that app.

What is the default launchMode in B4A? Does B4A use any flags regarding the launchMode when starting Activities?

Since which Android version there are these launchModes?
 
Upvote 0

Creideiki

Active Member
Licensed User
Hm... I tried with an Android 7 and an Android 10 device.
I used launchMode singleTask and got the same behavior as before.
Then I used launchMode standard (explicit in the Manifest) and... it works as expected.

Quit strange, I think... does B4A default to singleTask?
 
Upvote 0

Creideiki

Active Member
Licensed User
Yes, you're right... and singleTop should work as expected.
I have experimented a bit further.

It seems it has something to do with starting the app via the B4A-Bridge. Each time I load and start the app via bridge I get that problem (no matter if launchMode is standard or singleTop): When I start the app again via the home screen after leaving it in Activ2 it starts in the Main Activity (and tells me about killing previous instance), which gets the second Activity in the backstack. So when I press Back I get on Activ2 and another Back gets me on the home screen.
When I start the app again from the home screen and leave it in Activ2, I get in Activ2 when I start it from the home screen and no word about killing an instance.

So it seems only to be a problem when starting directly from the Bridge but not in production. So I can leave anything as it is. Problem solved.

Thanks for your help and pointing me in the right direction.
 
Upvote 0
Top