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).
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?
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
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...
...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?
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.
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.
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.
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.