onActivityResult没有被调用

时间:2013-02-03 07:20:35

标签: android android-intent android-activity onkeydown

当我从子活动返回时,不会在mainActivity中调用onActivityResult。我在儿童活动中使用finish()。你能告诉我原因吗?

先谢谢。

MainActivity

private void goToHomePage() { 
    if(connected){
        basket.putString(key, "dummy");
        Intent i = new Intent(Splash.this,HomePage.class);
        i.putExtras(basket);
        startActivityForResult(i,0);  
    }else{
        alert(); 
    } 
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data); 
    basket = data.getExtras();  
    Log.i("Splash","on activity result...");
    if (resultCode == RESULT_OK){  
        switch(requestCode){ 
        case 0:

            wifi.disConnect();
            break;
        }

    }
}

儿童活动

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.i("HomePage","onKeyDown");
        sendBack("close"); 
    }
    return super.onKeyDown(keyCode, event);
}

public void sendBack(String command) { 
    Intent sendBack = new Intent(); 
    Bundle basket1 =new Bundle();
    basket1.putString("command", command);  
    sendBack.putExtras(basket1);
    setResult(RESULT_OK, sendBack);
    finish();   
}

3 个答案:

答案 0 :(得分:0)

为什么有

    // TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data); 
onActivityResult中的

?我想这不是必需的,并且控件正在使用此语句并在逻辑之前退出方法。与其他注释一样,如果控制到达此语句,您是否已逐步检查调试?

答案 1 :(得分:0)

当用户按下后退键时,您是否尝试将某些内容传递给主要活动?在这种情况下,试试这个:

  1. 在儿童活动中,移除onKeyDown()
  2. 在儿童活动中,添加以下内容:

    @Override
    public void onBackPressed()
    {
        String command = "close";
    
        Intent sendBack = new Intent(); 
        Bundle basket1 =new Bundle();
        basket1.putString("command", command);  
        sendBack.putExtras(basket1);
        setResult(RESULT_OK, sendBack);
        finish(); 
        // Do not call super.onBackPressed() so as to ignore the default implementation.
    }
    

答案 2 :(得分:0)

这是我的错误,我在onAause of mainActivity中给了finish()。这会在活动转移到儿童活动时关闭活动。再次感谢!