Exit/Finish an app/activity - android

前端 未结 12 747
醉梦人生
醉梦人生 2021-02-05 05:04

I\'ve got 4 activities say Act1, Act2, Act3 and Act4. A button in Act1 opens Act2, a button in Act2 opens Act3, a button in A

12条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 05:41

    If you have another activity behind ( in the application activity stack), you could use finish() to exit the current activity.

    However, the back function too suppose to remove the current activity from your application activity stack.

    In my experience, it is very unreliable to rely on the activities in the back stack ( in the application activity stack). Because of that I used to exit each activity when I go deeper.

    In your case:

    Act 1 -> exit -> Act 2 -> exit -> Act 3 -> exit -> Act 4 -> exit -> Act 1 -> exit 
    

    The above method will exit all the activities.

    However, if you want to run some code in the background, it is better to rely on a "Service" rather than an "Activity". You can let the "Service" exit after doing its assigned work.

提交回复
热议问题