我正在使用一个简单的函数OnActivityResult,但是它没有返回我想要的结果。
请查看我的代码,并告诉我我在哪里做错了。
public void OnClickFunction(View view)
{
Intent intent = new Intent(getApplicationContext(), Second.class);
startActivityForResult(intent, RESULT_OK);
/// My actions. . .
}
然后在第二类中,我将Result设置如下:
Button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ValueOne = EditText.getText().toString().trim();
if (ValueOne.equals(String.valueOf(Answer)))
{
Toast.makeText(getApplicationContext(), "Correct Answer", 0).show();
Second.this.setResult(RESULT_OK, null);
Second.this.finish();
}
else
{
Toast.makeText(getApplicationContext(), "Wrong Answer", 0).show();
}
}
});
现在回到名为intent的first.class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == RESULT_OK)
// {
if (resultCode == RESULT_OK)
{
///// MyActions. . .
}
// }
}
调试器未在调试该功能,因此无法获得所需的结果。
我在哪里做错了?
最佳答案
您必须销毁第二项 Activity 。尝试按返回按钮。我可以在onActivityResult中看到所有日志消息
第一次 Activity
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
int result = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,SecondActivity.class);
startActivityForResult(i, result);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.i("H", "RequestCode:" + requestCode);
Log.i("H", "ResultCode:" + resultCode );
}
}
第二 Activity
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_OK);
Log.i("S","Exiting Second Activity");
}
}
关于android - OnActivityResult不起作用(Android),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8587769/