Exit/Finish an app/activity - android

前端 未结 12 934
醉梦人生
醉梦人生 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 06:06

    The approach I use is to start all child activities with startActivityForResult. Then I can putExtra("STATE", some_value) on exiting any child and use the state value to determine what to do up through the parents.

    If I want to exit the app from a deep child, "STATE" would be "exit" and each child in the hierarchy would simply get the StringExtra for "exit", do a putExtra("STATE", "exit") and call finish() and then the app would call finish() in the end. You can use this to achieve any desired state.

    Using startActivityForResult to pass messages this way avoids having to pass awkward references down into the child activities and simplifies your approach to setting the desired state of the app.

提交回复
热议问题