如何控制活动流 - 后退按钮与Home键按钮、Home

2023-09-12 04:36:39 作者:爱情的信仰

我在我的申请3项活动:

I have 3 activities in my application:

Activity1 -> Activity2 -> Activity3

里面Activity3,如果用户presses回,我想回到活性2。在Activity3的活动的onPause,我添加了一个完成()语句。这可能甚至不是必要的,但我想,以确保这一活动得到清理。这工作得很好。

Inside Activity3, if the user presses Back, I would like to return to Activity2. In Activity3's onPause event, I added a finish() statement. That's probably not even necessary, but I wanted to make sure this Activity gets cleaned up. This works fine.

不过,虽然在Activity3,如果用户presses主页或开始一个新的应用程序(通过通知栏或其他方式),我想无论Activity3和活性2完成。如果用户返回到该应用程序,他应该恢复与活动1。

However, while in Activity3, if the user presses Home or starts a new app (through notification bar or some other means), I want both Activity3 and Activity2 to finish. If the user returns to this app, he should resume with Activity1.

我已经找到了如何做一个或其他,但我不知道如何处理这两种情况下,如果它甚至有可能。我可以陷阱Activity3的后退按钮,发送消息回活性2告诉它不要完成()?这似乎是遵循着相同的生命周期流程(暂停,停止),无论你做什么把它们发送到后台。

I have figured out how to do one or the other, but I can't figure out how to handle both cases, if it's even possible. Can I trap the "Back" button in Activity3 and send a message back to Activity2 telling it not to finish()? It seems like the Activities follow the same lifecycle flow (Pause, Stop) regardless of what you do to send them to the background.

只是回答,为什么我想这种行为的问题,假设活动1 是一个登录界面,活性2 是选择画面, Activity3 是内容的屏幕。如果我preSS回到从内容页,我希望能够做出一个新的选择。如果通过任何其他方式我出口(首页,通知栏),我希望用户是注销

Just to answer the question of why I want this behavior, imagine that Activity1 is a login screen, Activity2 is a selection screen, and Activity3 is a content screen. If I press Back from the content page, I want to be able to make a new selection. If I exit via any other means (Home, notification bar), I want the user to be "logged out".

在此先感谢您的帮助。

推荐答案

再次,我已经回答了我的问题。我会后我的解决方案在此情况下,它可以帮助别人。

Once again, I have answered my own question. I'll post my solution here in case it helps someone else.

在这两种活性2和Activity3的的onPause事件,我添加完成()。这需要照顾的情况下,用户presses主页或响应一个通知栏的事件,而这些活动。由于这些活动都完成后,如果用户返回到该应用,他们将得到活性1(现在堆栈的顶部。)

In the onPause events of both Activity2 and Activity3, I added finish(). This takes care of the case where the user presses Home or responds to a Notification Bar event while in those activities. Since these activities are both finished, if the user returns to the app, they will get Activity1 (now at the top of the stack.)

在Activity3,我增加了一个的onkeydown陷阱返回键。当它来到的onPause由于活性2被打死,我们要火了从Activity3一个新的活性2。启动活性2后,Activity3然后完成。这里的$ C $下Activity3的的onkeydown:

In Activity3, I added an onKeyDown trap for the "Back" key. Since Activity2 was killed when it went onPause, we have to fire off a new Activity2 from Activity3. After starting Activity2, Activity3 then finishes. Here's the code for Activity3's onKeyDown:

public boolean onKeyDown(int keyCode, KeyEvent event){
    if(keyCode == KeyEvent.KEYCODE_BACK) {
            Intent Act2Intent = new Intent(thisActivity, Activity2.class);              
            startActivity(Act2Intent);          
            finish();
            return true;
    }
    return false;
}
 
精彩推荐
图片推荐