简体   繁体   English

Android从主屏幕还原应用程序时,切换到其他活动

[英]Android When restoring app from homescreen, switch to different activity

I have three activities, lets call them Act1, Act2 and Act3. 我有三个活动,我们称它们为Act1,Act2和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). Act1上有一个登录表单(成功登录后会转到Act2),该表单有两个选项(1.转到Act3,转到带有一些额外数据的Act3)。 The user then goes to Act3. 然后,用户转到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). 当然,当用户按下android设备的“主页”按钮时,该应用程序将最小化并保存在内存中,直到android需要使用内存(在这种情况下,该App被销毁)。 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. 我希望能够最小化该应用程序,然后重新打开该应用程序后,直接转到Act1以提示用户再次登录。 I do not want the app to be able to resume in Act2 or Act3. 我不希望该应用程序能够在Act2或Act3中恢复。

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. 以本机GMail应用程序为例:它不需要每次打开时都重新进行身份验证。

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. 现在,如果您的应用程序确实需要这种行为(例如,这是信用卡安全之类的东西),那么我的第一个猜测就是处理Act3的onPause()并从那里调用finish()。 Just be careful not to call finish() twice (see isFinishing()). 请注意不要两次调用finish()(请参阅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. 另外,由于这违反了用户的期望,因此请向用户明确说明您的应用程序出于安全性的考虑而行事,而不是因为它不想让人讨厌。

When the user moves away from your activity (pressing the home button for example), the onPause() method is called first. 当用户离开您的活动(例如,按下主页按钮)时,将首先调用onPause()方法。 You should be able to handle your logic there (for example, calling finish() on Act2 or Act3). 您应该能够在那里处理逻辑(例如,在Act2或Act3上调用finish() )。

Edit: heh, yeah, what he said :D 编辑:嘿,是的,他说的是:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM