当ActionBar中点击返回按钮时,onActivityResult未被调用

4
这里是我的问题:
  1. Create a MainActivity. Add a button which will start another activity SecondActivity.

            Intent i = new Intent(getActivity(),SecondActivity.class);
            startActivityForResult(i,0);
    
  2. Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.

    When back button in action bar is clicked:

@Override

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            // back button
            Intent resultIntent = new Intent();
            // TODO Add extras or a data URI to this intent as appropriate.
            setResult(Activity.RESULT_OK, resultIntent);
            //finish();
            return false;

    }
    return super.onOptionsItemSelected(item);
}

当活动内的按钮被点击时:

Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent resultIntent = new Intent();
        // TODO Add extras or a data URI to this intent as appropriate.
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }
});

当我在SecondActivity内点击按钮时,MainActivity中的onActivityResult被调用,但如果我点击SecondActivity中的Actionbar中的返回按钮,则该方法从未被调用。有人能告诉我原因吗?谢谢。


在onOptionsItemSelected()中取消注释finish()并尝试一次... - Gopal Gopi
我尝试过使用finish()和不使用finish(),但都没有起作用。 - Bagusflyer
"return true;" 是该问题的真正关键字。如果您忽略了它,应用程序将无法正确关闭。 - TeachMeJava
3个回答

4

以下是可用的代码:

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            // back button
            Intent resultIntent = new Intent();
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
            return true;

    }
    return super.onOptionsItemSelected(item);
}

我猜测finish()会关闭当前的Activity,并返回true表示动作已经处理完毕。(默认的返回动作似乎与finish()不同。)

1
如果我在onOptionsItemSelected中返回false,那么MainActivity中的onCreate事件将被触发。MainActivity已经重新创建了。我猜这就是为什么onActivityResult从未被调用的原因。 - Bagusflyer

1

好的答案是Gopal Rao在同一个问题中的代码。这对我有用。这是他解决方案的副本:

    public boolean onOptionsItemSelected(MenuItem item) {
       if (item.getItemId() == android.R.id.home) {
          Intent result = new Intent((String) null);
          result.putExtra("SOME_CONSTANT_NAME", true);
          setResult(RESULT_OK, result);
          finish();
          return true;
       } 
       else {
          return super.onOptionsItemSelected(item);
       }
    }

1
尝试这个:-
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        Intent resultIntent = new Intent();
        setResult(Activity.RESULT_OK, resultIntent);
        onBackPressed();
        return true;
}
return super.onOptionsItemSelected(item);
}

我猜这和我的答案是一样的。finish() 和 onBackPressed() 之间有什么区别吗? - Bagusflyer
它对我有效,所以我想可能也适用于你的情况,值得一试。 - Hardip Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接