我想知道如何在Android上返回上一个屏幕。不只是回去。我通常在智能手机上点击"后退" 按钮,但通常存在或带我回到家时,它应该把我带到上一个屏幕。
我想知道如何在Android上返回上一个屏幕。不只是回去。我通常在智能手机上点击"后退" 按钮,但通常存在或带我回到家时,它应该把我带到上一个屏幕。
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.
这是一个过度简化的东西,但简单的答案是:你不能。
是什么使这一项过度简化?嗯,这是因为它取决于如何实现给定的应用程序。 Android应用程序基于活动的概念,该概念在开发文档中描述为:
一个活动是用户可以做的单一的,重点的东西。
当启动新活动时,它将其放置在堆栈顶部的系统范围内的"活动堆栈" (对于任何人不熟悉,请参阅堆栈(数据结构))。按下后按按钮,当前活动(堆栈的顶部)完成,并从堆栈中删除,导致前一个(堆叠在堆栈上)被带到前景。当您继续按下后按钮之前,此弹出/带入前景行为仍在继续,直到您在家中降落,这是堆栈的开始。从这里,你不能再去"回来" 。
现在,这是如何适用于应用的?在许多应用中,活动代表一个,单个屏幕。当您向新屏幕提升时,实际上为屏幕创建了一个新的活动,并将其推堆叠在创建它的堆栈上。在使用这种设计的应用中,击中后面的屏幕将带您到上一个屏幕。原因应该是相当不言而喻的:您当前正在查看的屏幕位于堆栈的顶部,上一个屏幕坐在下面(因为它们是每个活动本身)。因此,击中后退从堆栈中删除当前屏幕,完成它,然后加载前一个。
这不是必需的模型。您可以具有包含多个布局和屏幕的活动。我们在我的办公室里使用了它,例如,创建一个配置向导。这个想法是击中后面应该只是退出向导,并且屏幕本身上有"返回" 和"下一个" 按钮来提供导航。因此,即使您在向导的第三个屏幕上,您尚未创建任何其他活动,因此击中后按钮(硬按钮)将从"向导活动" 中带出并返回"菜单活动" 。
更进一步地,后按按钮的行为可以通过应用程序的应用来覆盖。它只需调用一个名为 onBackPressed()
的方法,该方法在活动堆栈上执行返回的默认行为。但是,如果在当前活动中存在 onBackPressed()
的覆盖实现,则将执行。
所以总结:根本不能保证背面按钮返回上一个"屏幕" 的想法。硬回合按钮处理活动仅,它们通常是"屏幕" 给用户,但不一定是,绝不需要。除非您使用的应用程序说,否则没有"返回一个屏幕" 的概念,但仍然没有保证由后退按钮本身提供的功能。
进一步阅读:
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:
如果一个应用程序在击中时不带您到上一个屏幕,那么你或Android就可以了解它。您可以要求应用程序的开发人员实现它,但这是关于它的。
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.
© 2022 it.wenda123.org All Rights Reserved. 问答之家 版权所有