Please have a look at the graph: https://i.stack.imgur.com/G2lfN.png
I have some trouble with the architecture of my app. I have a few Activity/states that if you reach them you are not allowed to go back. These are K, A and X, so when you reach them and press back button you will close my app.
It is very easy with START Activity. As soon as I do some work to decide if I go to K or A, I just run START.finish() and that is what I want (being in K or A Activity and pressing back button closes app as START Activity no longer exist).
The problem starts with X state. The application is like you need to fullfill 3 steps so you go through:
[START]->[A<->B<->C]->[X]
The requirements are while pressing back button:
on A: closes app
on B: show A
on C: show B
If you then finally finish some kind of wizard (A->B->C) you reach X where if you only press back button you closes app.
QUESTION: How should I finish() A, B, C to prevent back button from X? I have no reference to use.
One way to achieve this, is in activity X, make pressing the back button fire of a custom event.
You’ll notice the above method has the line Globals.finished = true; This refers to a class called Global with a static (static can be called on a class without having to instantiate an object) boolean variable called finished that is initially set to false as follows. In my own apps I often use a Globals class for sharing things common to the whole app. You can also used the shared preferences to do somthing similar.
Then in the onResume (or possibly onStart) lifecycle method of all activities you can put the following
This will cause, all the activities to immediately close, but only once the finish variable in the Globals is true. I’ve done this before and it works quite well and will ensure the activities call their life cycle methods on closing.
Even simpler than this, you could avoid the use of the back button event handler, by setting the Globals.finished = true, in the onClose method of ActivityX.