Search code examples
androidandroid-intentandroid-activityback-button

Android Back button with many activities


I have a Main-activity that starts a Menu-activity, and then 4 buttons in there: Test-button, Create-button, Result-button and Logout-button.

The first Button test starts a test-activity and the test-activity starts a process-activity and the process activity starts a endResult-activity. After the endResult-activity I want to go back to the Menu-activity. I don't know if this is possible or if I have to many activities. I have tried to use finish() after starting new activity and it works; I get back to the Menu-activity, but if I want to do this again it crashes when I try to go to the Test-Activity again.

All answers and tips are much appreciated!


Solution

  • Here is how I would solve it:

    Instead of having separate activities for test-activity, process-activity, and endResult-activity, just use one activity with a FrameLayout that you can use to dynamically add fragments. Change test-activity, process-activity, and endResult-activity to fragments, and use FragmentTransactions to load each one in succession, but don't add to the back stack.

    As a result, after the user is finished with endResult-activity, a back click will take them back to MenuActivity.