Android Question StartActivity followed by Activity.Finish

wimpie3

Well-Known Member
Licensed User
Longtime User
I want to start a new activity and close the current activity at the same time (to prevent users going back to the current activity once they are in the new one).

I'm using this code:

B4X:
StartActivity(name_of_new_activity)
Activity.Finish

However, it seems that in about 50% of the cases, Activity.Finish destroys the current activity without having had the chance to start the new activity. This means the program just quits instead of going to the next activity.

Is there something Activity.Finish should wait for, before I fire this function?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is most probably not related to the Activity.Finish call.

Try to start the activity with CallSubDelayed instead of StartActivity.

There are cases where Android ignores the intent (it happens when you switch between several activities quickly). CallSubDelayed will retry to start the activity several times.

B4X:
CallSubDelayed(name_of_new_activity, "any sub you like")
Activity.Finish
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I've never experienced such behavior. Under which sub type are you setting these commands?
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
The behavior can easily be reproduced by the attached program. If you click slowly on the buttons, everything works okay. As soon as you tap a little faster, the initial screen appears again (which should normally NEVER happen if you follow the logic of the program).

I have the impression that callsubdelayed does not change anything, but Erel might find a better solution to circumvent this problem...
 

Attachments

  • finishbug.zip
    6.6 KB · Views: 242
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
This is the version with CallSubDelayed: exactly the same result! The "unwanted" activity pops up when you click fast on the buttons.
 

Attachments

  • finishbug.zip
    6.6 KB · Views: 210
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
...and adding an animation on the screen makes things even worse. See the attached source code. You can flip between the two screens exactly ONCE. The next flip fails... ALWAYS!

Either a bug in B4A, or in Android, or... something else I don't see?
 

Attachments

  • finishbug.zip
    6.3 KB · Views: 229
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
Please try the example from post 6. The one from post 5 only fails when you click on the buttons really quick. The example from post 6 fails ALL the time on my mobile.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I've tried both examples, no such thing happens. What's your testing device?
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
HTC Desire
Android 2.3.3

Erel, I have the same file attached with CallSubDelayed this time. The problem is the same: I can click once on the button to launch screen2, go back to the main screen, but clicking on the button to show screen2 once again results in an application exit.
 

Attachments

  • finishbug.zip
    6.3 KB · Views: 251
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I tested your app on 2 devices. Here are my findings:
1. A device with OS 4.0.3. The app behaves exactly as Wimpie reported. It exits the app on 2nd click.
2. A device with OS 4.2.2. The app works as it was supposed to without exiting and as reported by Erel.
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
Once I remove Activity.Finish, the app works. But not the way I want. I *absolutely* need to finish the activity. This "bug" seems to be related to "older" versions of Android, I cannot reproduce it on 4.2.2 (as confirmed before by Mahares). Desperately looking for a workaround for older versions.
 
Upvote 0

alan dodd

Member
Licensed User
Longtime User
Hello, wimpie3. Just for info: I can confirm the exact behavour as you describe it:

with android 4.0.3 (on a dolphin black 60E Honeywell) it exits on 2nd click
with android 5.02 (on htc one) it works forever

with android 4.0.3 adding the line you mentioned to the manifest, it works forever

regards, Alan
 
Upvote 0
Top