Navigating Android: How To Go Back To Previous Screen? I would like to know how to return to previous screen on Android. Not just go back. I usually tap "Back" button on smartphone but often it just exists or takes me back to home when it should have taken me to previous screen.
This is something of an oversimplification, but the simple answer is: you can't.

What makes this an oversimplification? Well, it's because it depends on how a given app is implemented. Android applications are based around the concept of an Activity, which is described in the development documentation as:


  An activity is a single, focused thing that the user can do.


When a new Activity is launched it is placed onto a system-wide "Activity stack" at the top of the stack (for anyone unfamiliar, see Stack (data structure)). When the back button is pressed, the current Activity (at the top of the stack) is finished and removed from the stack, causing the previous one (beneath it on the stack) to be brought to the foreground. This pop/bring-to-foreground behavior continues as you keep pressing the back button until you land at HOME, which is the start of the stack. From here, you cannot go "back" any further.

Now, how does this apply to apps? In many applications, an Activity represents one, individual screen. When you advance to a new screen, then, a new Activity is actually created for the screen and pushed onto the stack on top of the one that created it. In apps that use this design, hitting back will, in fact, bring you to the previous screen. The reason should be fairly self-evident: the screen you are currently viewing is on the top of the stack, the previous screen sits below it (since they are each Activities themselves). Thus, hitting back removes the current screen from the stack, finishes it, and then loads the previous one.

This is not a required model, however. You can have an Activity that contains multiple layouts and screens. We have used this in my office to, for example, create a configuration wizard of sorts. The idea is that hitting back should simply exit the wizard, and there are "Back" and "Next" buttons on the screen itself to provide navigation. Thus, even if you are on the third screen of the wizard, you have not created any additional Activities, so hitting the back button (hard button) will take you out of the "Wizard Activity" and back to the "Menu Activity".

Further, the behavior of the back button can be overridden by an application of they so choose. It simply calls a method named onBackPressed(), which performs the default behavior of going backwards on the Activity stack. However, if there is an overridden implementation of onBackPressed() in the current Activity, that will be executed instead.

So to sum up: the idea that the back button goes back to the previous "screen" is not guaranteed at all. The hard back button deals with Activities only, which are often "screens" to the user, but aren't necessarily, and are in no way required to be. There is no concept of "go back one screen" unless the app you are using says there is, but that functionality is still not guaranteed to be provided by the back button itself.

Further reading:


Activity and Task Design
Activity documentation