返回键检测到,但没有活动结束 [英] back key detected but Activity not ending

查看:133
本文介绍了返回键检测到,但没有活动结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是让我有点疯狂。

我有以下应用:

活动A - >活动B - >活动ç

Activity A -> Activity B -> Activity C

一个在表现为Android的定义:launchMode =singleTask

A is defined in the manifest as android:launchMode="singleTask"

B开始C作为如下:

        Intent startActivity = new Intent();
        startActivity.setClass(this,C.class);
        startActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(startActivity);

ç会谈来回通过套接字远程系统。

C talks back and forth with a remote system over a socket.

虽然C的通信,返回键被禁用。这里是C的的onkeydown():

While C is communicating, the Back key is disabled. Here is C's onKeyDown():

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (sessionActive() && (keyCode == KeyEvent.KEYCODE_BACK)) {

        return false;
    }
    else if (!sessionActive() && (keyCode == KeyEvent.KEYCODE_BACK)){

            Log.d("C","done talking to socket, got Back key, exit Activity");
        /* delete data from an internal static object */
    }

    return super.onKeyDown(keyCode, event);
}

sessionActive()如果我们仍然在插槽说话返回true,我们要返回键禁用。

sessionActive() returns true if we are still talking over the socket and we want the Back key disabled.

在sessionActive()是假的,我们说完,我们希望返回键退出活动℃。

When sessionActive() is false we are done talking and we WANT the Back key to exit Activity C.

问题是,它不工作。我已经通过日志消息,并第一时间sessionActivity()==遵循这个假我得到了说完到插座的消息,所以我知道我得到正确的地方。但是,我不得不preSS返回一个第2次,以获得活动C至消失。

The problem is, its not working. I've followed this via log messages and the 1st time sessionActivity() == false I do get the "done talking to socket" message so I KNOW I'm getting to the right place. But, I have to press Back a 2nd time in order to get Activity C to go away.

任何想法?

更新来更新:

我已经试过这两个答案,并已经得到了同样的混乱的结果。

I have tried both answers and have gotten the same confusing results.

下面是一个有点我的logcat中显示的diffent生命周期方法的执行。我公司拥有一批在logcat中穿插的文字问题。

Here is a bit of my logcat showing the execution of the diffent lifecycle methods. I have a number of questions interspersed in the logcat text.

C.onCreate() 
C.onStart() 
C.onResume() 
C.onKeyDown() sessionActive()==false, keyCode = BACK              
                          this should get us out but doesn't

C.finish() sessionActive is FALSE                                
                          shouldn't this go to onStop()?

C.onPause() sessionActive is FALSE
C.onRestart() sessionActive is FALSE                              <-  ???????? onRestart?
C.onStart() sessionActive is FALSE                                <-  ???????? onStart?
C.onResume() sessionActive is FALSE
C.onStop() sessionActive is FALSE                                 <- this makes sense
C.onDestroy() sessionActive is FALSE                           <- so does this
C.onPause() sessionActive is FALSE                             <- ????????
C.onResume() sessionActive is FALSE

C.onKeyDown() sessionActive is FALSE, keyCode = BACK
                        this is 2nd back press that does exit
C.finish() sessionActive is FALSE
C.onPause() sessionActive is FALSE                there is no onRestart here!!!!!!!!!
C.onStop() sessionActive is FALSE
C.onDestroy() sessionActive is FALSE

我不明白的控制流。我还以为完成()发送控制的onDestroy(),我们会完成。

I don't understand the flow of control. I would have thought finish() sent control to onDestroy() and we'd be done.

推荐答案

您可以试试:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ( keyCode == KeyEvent.KEYCODE_BACK )
    {
        if ( sessionActive() )
            return false;

        Log.d("C","done talking to socket, got Back key, exit Activity");
        /* delete data from an internal static object */
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

这篇关于返回键检测到,但没有活动结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆