Android 从主屏幕恢复应用程序时,切换到不同的活动
我有三个活动,我们称它们为 Act1、Act2 和 Act3。 Act1 上有一个登录表单(成功登录后)会转到 Act2,其中有两个选项(1. 转到 Act3,转到带有一些额外数据的 Act3)。然后用户转到Act3。
当然,当用户按下 Android 设备的“home”按钮时,应用程序将最小化并保存在内存中,直到 Android 需要使用内存(在这种情况下应用程序将被销毁)。但是,当用户按下“home”按钮然后再次快速打开应用程序时,应用程序将恢复到最小化之前位于前台的 Activity。
我希望能够最小化应用程序,然后重新打开后直接进入 Act1 以提示用户再次登录。我不希望应用程序能够在 Act2 或 Act3 中恢复。
I have three activities, lets call them Act1, Act2 and Act3. There is a login form on Act1 which (upon successful login) goes to Act2 which has two options (1. Go to Act3, Go to Act3 with some extra data). The user then goes to Act3.
Of course, when the user presses the "home" button the android device, the application is minimized and held in memory until android needs to use the memory (in which case the App is destroyed). However, when the user presses the "home" button and then opens the app up again quickly, the app is restored to the Activity that was in the foreground before it was minimized.
I want to be able to minimize the app, and then once re-opened go straight to Act1 to prompt the user to login again. I do not want the app to be able to resume in Act2 or Act3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您的应用程序确实对安全敏感,否则默认行为应该对用户更好:每次启动应用程序时输入登录名和密码可能会非常烦人。以本机 GMail 应用程序为例:它不需要您在每次打开时重新进行身份验证。
现在,如果您的应用程序确实需要这种行为(比如说它是信用卡安全或类似的东西),那么我的第一个猜测是处理 Act3 的 onPause() 并从那里调用 finish() 。请注意不要调用 finish() 两次(请参阅 isFinishing())。
另外,由于这违背了用户的期望,因此请向用户明确您的应用程序的行为是为了他们的安全,而不是因为它想让用户感到厌烦。
Unless your application is really security-sensitive, the default behavior should be better for the user: typing their login and password every single time they launch the app can be very annoying. Take for example the native GMail application: it doesn't require you to reauthenticate every time it opens.
Now, if your application really needs that behavior (say it's a credit card safe or something like that), then my first guess would be to handle Act3's onPause() and call finish() from there. Just be careful not to call finish() twice (see isFinishing()).
Also, since this is a breakage of the user's expectations, make it clear to the user that your app behaves like that for their security, not because it wants to be annoying.
当用户离开您的 Activity 时(例如按主页按钮),首先调用
onPause()
方法。您应该能够在那里处理您的逻辑(例如,在 Act2 或 Act3 上调用finish()
)。编辑:嘿,是的,他说的是:D
When the user moves away from your activity (pressing the home button for example), the
onPause()
method is called first. You should be able to handle your logic there (for example, callingfinish()
on Act2 or Act3).Edit: heh, yeah, what he said :D