20

这是我的问题,我的主视图只显示一个按钮,按下此按钮会显示另一个视图。此视图只有另一个按钮,当按下此按钮时,当前视图完成并且控件返回到前一个视图。

为了显示我使用 startActivityForResult 的第二个视图,我将代码放在这里。

private void startNewview() {       
    Intent it = new Intent(getApplicationContext(), newView.class);
    startActivityForResult(it,VIEW_ID);

}

调用的视图只有一个按钮事件,这里是代码

Button b = (Button) findViewById(R.id.close);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            setResult(RESULT_OK);               
            finish();

        }
    });

最后,主视图中的onActivityResult方法,这里是代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == VIEW_ID && resultCode == RESULT_OK) {
        tv = (TextView) findViewById(R.id.tv);
        tv.setText("The result ok is  here :)");
    }

}

问题是 resultCode 总是 0 = RESULT_CANCELED 并且我不知道如何解决它,有人可以帮助我吗?

非常感谢你!

4

3 回答 3

10

这里,

@Override
public void onBackPressed() {
    setResult(Activity.RESULT_OK);
    finish();
}

确实可以通过按 BACK 按钮返回(RESULT_OK)。不要打电话

super.onBackPressed().

于 2016-02-06T14:07:31.720 回答
7

我无法解释您的代码中发生了什么,但我有一个项目示例来执行此操作..

一个只有一个按钮 btnFoo 的 FooActivity:

@Override
protected void onStart() {
    super.onStart();

    btnFoo.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent("xper.activity.ACTIVITY_BAR_INTENT"),1);
        }
    });
}

并在 AndroidManifest.xml 中添加了一个 BarActivity,如下所示:

<activity
    android:name = "BarActivity">
    <intent-filter>
        <action
            android:name = "xper.activity.ACTIVITY_BAR_INTENT"/>
        <category
            android:name = "android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

检索条内意图的相应代码位于 btnBar (Button) 的 onClicEvent 中:

@Override
protected void onStart() {
    super.onStart();

    btnBar.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
            intent.putExtra("codBar", "bar");
            setResult(Activity.RESULT_OK, intent);
            finish();

        }
    });
}

现在,如果您没有很好地处理 onActivityResult() 事件,当您按下 Android 按钮“BACK”时,您可能会收到错误消息。

如果Activity B中的Intent(意图)是给Activity A一些信息,如果你按下按钮返回,不知道Activity B会不会入栈,但是Intent没有做。所以我做了以下事情:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    //Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
    //intent.putExtra("codBar", "bar");
    //setResult(Activity.RESULT_CANCELED, intent);
    setResult(Activity.RESULT_CANCELED);
    finish();
}

处理信息我在事件 onActivityResult() 中做了以下操作,以查看 Bar Activity 中检索到的信息:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(data != null) {
        Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode + "\tdata == " + data, 10000).show();
        btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode + "\tdata == " + data /*+ "extras == " + data.getExtras().getString("codBar")*/);
    } else {
        Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode, 10000).show();
        btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode);
    }

}

如果您有更多活动将信息返回给父活动是很好的做法,请执行以下操作:

//put private static final int globals atributes with the respective name of the
//activity to represent the requestCode for each activity you have like:
private static final int ACTIVITY1 = 117;
private static final int ACTIVITY2 = 118;
...
private static final int ACTIVITYN = 215;

//In the event onActivityResult() is better use the switch statement to handle each
//specific activity to catch information
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_CANCELED) return; // breaks
    //if you decide to handle some information of Activity.RESULT_CANCELED
    //ignore the above condition that returns and handle it inside the switch statement

    switch(requestCode) {
    case ACTIVITY1:
    {
        //Dosomething
    } break;
    case ACTIVITY2:
    {
        //Dosomething
    } break;
    ...
    case ACTIVITYN:
    {
        //Dosomething
    } break;
    }
}

如果您不能执行此示例代码.. 请给我您的电子邮件,让我发送 FooBarActivity 项目

于 2012-02-22T15:08:58.270 回答
-1

用这个

  Intent returnIntent = new Intent();
  setResult(RESULT_OK,returnIntent); 

代替

setResult(RESULT_OK);

只要

于 2011-02-04T12:43:25.593 回答