5

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.

0

2 Answers 2

5

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:

1

If an app doesn't take you to the previous screen when you hit Back, there's nothing you or Android can do about it. You could ask the app's developer to implement it but that's about it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .