1

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

提前致谢。

主要活动

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();   
}
4

3 回答 3

0

为什么你有

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

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

于 2013-02-03T08:28:23.790 回答
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.
    }
    
于 2013-02-03T08:48:32.743 回答
0

这是我的错误,我在mainActivity的onPause中给出了finish()。当它移动到子活动时,它会关闭活动。再次感谢 !

于 2013-02-08T03:26:47.793 回答