MrGibbage
MrGibbage

Reputation: 2646

Android back button should behave as an UP button

My app has several activities which can be thought of as separate levels in a hierarchy. You choose an object in activity A, which takes you to activity B, where you can add, modify and delete some level C parameters, etc. If I had traversed A->B->C, saved my changes in C which automatically takes me back to level B, and then pressed the back button, it would take me back to Activity C, because that was the last activity seen. Now, I know what you are going to say. Use android:noHistory="true". Well, I can't use that because some of these activities launch dialogs and when you close a dialog, android takes you back to the last "regular" (that is, noHistory="false") activity. I have tried overriding the onBackPressed, which works until the app exits. I have also tried android:finishOnTaskLaunch="true", and it just doesn't work as the documentation says. My app will start on activities with that setting after restarting.

I would like it if the back button always acted as an up button. It should never take you down a level. Only up.

I am targeting API level 8. It seems that api 11 has some options for clearing the history stack. Is there anything I can do in API 8 to make it work right?

Upvotes: 0

Views: 492

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

If I had traversed A->B->C, saved my changes in C which automatically takes me back to level B, and then pressed the back button, it would take me back to Activity C, because that was the last activity seen.

Have C call finish() after it "automatically takes me back to level B" (presumably by some call to startActivity()). Then, pressing BACK from B will return the user to A.

I would like it if the back button always acted as an up button. It should never take you down a level. Only up.

Then finish() any "down" activities when you are done with them, if the user did not do that themselves by exiting them by pressing BACK.

Upvotes: 3

Related Questions